A simple excursion on jQuery’s promises

Using Kriskoval‘s q version of promise I was in trouble when I required promises for a browser application based on jQuery (+more). After a while I found it exists promise in jQuery too, and is not so different in use. (ok, Kriskoval would arguments on this, and of course he is right, but it is not so different from a [coder-]user point of view)

function myRequest() {
  var dfd = jQuery.Deferred();
  yourAsync(par, par, dfd);
  return dfd;


and then 


function yourAsync(par, par, dfd) {
 ///do it
	// callback function ....
        // at the end call

and use it as a promise

   console.log('all done');
}).fail(function() {

It also integrate well with the rest of jQuery async call, such as $.ajax:

var req = $.ajax({url:. .., ...}); 
$.when(req).then(function() { .... });

the $.when and deferred object are well documented in jQuery site with examples too

That’s all folks!

Photo credits: Christian Ditaputratama original image: link