I think the easiest way to do this is to keep a secondary Deferred
object around for each AJAX request, and ensure that that one is always resolved:
var d1 = $.Deferred();
var d2 = $.Deferred();
var j1 = $.getJSON(...).complete(d1.resolve);
var j2 = $.getJSON(...).complete(d2.resolve);
$.when(j1, j2).done(function() {
// only fires if j1 AND j2 are resolved
});
$.when(d1, d2).done(function() {
// will fire when j1 AND j2 are both resolved OR rejected
// check j1.isResolved() and j2.isResolved() to find which failed
});
This is making use of the additional AJAX .complete()
method which jQuery adds to its promises for AJAX methods, which is called for both resolved and rejected promises.
NB: d1.resolve
works as a callback in its own right, it doesn’t need to be wrapped in a function() { ... }
block.