jQuery Deferred’s, $.when() and the fail() callback arguments

$.when() will execute the failed callback (2nd parameter passed to then()) immediately if any one of the parameters fails. It’s by design. To quote the documentation:


In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

There’s actually no built-in way of getting a callback that waits untils all of them are finished regardless of their success/failure status.

So, I built a $.whenAll() for you 🙂
It always waits until all of them resolve, one way or the other:


$.whenAll(a, b, c)
    .then( callbackUponAllResolvedOrRejected );

Leave a Comment