With jQuery.when()
, if one request fails then they all fail.
$.when(deferred1, deferred2).then(function() {
// Called only after both deferred are resolved
}, function() {
// Called immediately after one of the deferreds is rejected
});
How can you wait for everything to execute even failures? I tried to use .always()
instead of .then()
but the callback is also being called right after the first failure.
One (ugly) solution would be to have flags to control when both operations are completed and use the .always()
on each of them (without $.when()
).
var deferred1Complete = false;
var deferred2Complete = false;
function callback() {
if (deferred1Complete && deferred2Complete) {
// Execute after both deferred are either resolved or rejected
}
}
deferred1.always(function() {
deferred1Complete = true;
callback();
});
deferred2.always(function() {
deferred2Complete = true;
callback();
});
Is there a better way to do this?