One use of jQuery's Deferred
system is with the $.when()
function. It takes a variable number of Promise's and will do something when they all
resolve(or when the first
reject`s.)
So if you wanted to operated on two Ajax JSON
queries for instance you could do:
var funkyPromise = $.getJSON('http://funky.json.service.com');
var awesomePromise = $.getJSON('http://awesome.json.service.com');
$.when(funkyPromise, awesomePromise).then(function() {
/* do something amazing with the two objects */
}, function() {
/* at least one of the services failed this time */
});
Another thing you can do with jQuery's Deferred
system is to make data pipelines by "chaining" the data from one Deferred
through another using the pipe
method before ultimately using it:
$.getJSON('http://funky.json.service.com')
.pipe(funkytoFunkier);
}).done(function(funkyData) {
/* we now have a funkier version of what the web service gave us */
});
Everything wonderfully asynchronous and decoupled.
But what happens if I want to use $.when()
on two asynchronous Promise
s but we don't have one of them yet because it will come through a pipe
asynchronously?
var funkyPromise = $.getJSON('http://funky.json.service.com');
var awesomePromise = $.getJSON('http://awesome.json.service.com');
// run "funky" through an asynchronous "pipe" to get "funkier"
//
// ... but ... how ??
$.when(funkyier, awesome).then(function() {
/* do something amazing with the two objects */
}, function() {
/* at least one of the services failed this time */
});
So what goes in the middle section?
pipe()
or $.when()
etc make it easier?pipe() returns a new promise that will be resolved when the pipe is resolved, so you only have to write:
var funkyPromise = $.getJSON('http://funky.json.service.com');
var awesomePromise = $.getJSON('http://awesome.json.service.com');
$.when(funkyPromise.pipe(funkytoFunkier), awesomePromise).then(function() {
/* do something amazing with the two objects */
}, function() {
/* at least one of the services failed this time */
});
Note that, starting with jQuery 1.8, then()
does the same thing as pipe()
, so the two methods are basically interchangeable.