Joining promises with $.when

$.when is another method that can accept multiple promises and return a master deferred object. This master object can be resolved if all the promises are resolved, or it would be rejected if any of the promises were rejected. You may have sequence like when().then().done() or you can add multiple when() methods followed by then() and done().

Let's take a look at an example of how the code will look like with $when():

<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.0.js"></script> <script> function getActorByRating(id){ var d = $.Deferred(); $.post( "/echo/json/", {json: JSON.stringify({firstName: "Arnold", lastName: "Schwarzenegger", rating: "8.0"})} ).done(function(p){ d.resolve(p); }).fail(d.reject); ...

Get Mastering JavaScript Promises now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.