Reputation: 116263
Deferred
objects have two main pools of callbacks, namely doneCallbacks
and failCallbacks
. Both pools are "linear": the callbacks are stored one after the other in the order they were given in.
This linear structure seems to go against the "tree-like" structure one has to consider when handling errors. At every step, there are two cases: fail and pass.
if(err) {
// stuff
if(err) {
// stuff
} else {
// stuff
}
else {
// stuff
if(err) {
// stuff
} else {
// stuff
}
}
It seems that because of the imposed linearity of Deferred
s, they are not very suited for error handling. Am I overlooking something?
Upvotes: 2
Views: 789
Reputation: 75993
It's a bit wordy but this is from the jQuery documentation for $.when()
:
In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, it is passed the resolved values of all the Deferreds that were passed to jQuery.when. For example, when the Deferreds are jQuery.ajax() requests, the arguments will be the jqXHR objects for the requests, in the order they were given in the argument list.
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.
Source: http://api.jquery.com/jquery.when/
So if a deferred
object resolves to an error then the master deferred
fires failCallbacks
and it's up to you to decide what to do with the possibly un-resolved deferred
s.
Upvotes: 5