Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  11] [ 5]  / answers: 1 / hits: 26565  / 13 Years ago, fri, april 1, 2011, 12:00:00

I'm getting an unexpected result when using $.when() when one of the deferred operations does not succeed.



Take this JavaScript, which created 2 deferreds. The first one succeeds and the second one fails.



var f1 = function() {
return $.Deferred(function(dfd) {
dfd.resolve('123 from f1');
}).promise();
};

var f2 = function() {
return $.Deferred(function(dfd) {
dfd.reject('456 from f2');
}).promise();
};

$.when(f1(), f2())
.then(function(f1Val, f2Val) {
alert('success! f1, f2: ' + JSON.stringify([f1Val, f2Val]));
})
.fail(function(f1Val, f2Val) {
alert('fail! f1, f2: ' + JSON.stringify([f1Val, f2Val]));
});


Run it yourself: http://jsfiddle.net/r2d3j/2/



I get fail! f1, f2: [456 from f2, null]



The problem is that in the .fail() callback the value passed with the f2() rejection, is being routed to the first argument, where i expect the f1Value. Which means that I don't really have a way of know which deferred object actually posted that reject(), and I also dont know which operation that failure data actually belongs to.



I would have expected that .fail() would get arguments null, '456 from f2' since the first deferred did not fail. Or am I just not doing deferreds right way here?



How do I know which deferreds failed, and which rejection arguments belong to which failed deferred if the argument order in the callback is not respected?


More From » jquery

 Answers
153

Internally, the reject and fail paths are handled by two totally separate queues, so it just doesn't work the way you expect.



In order to know which original Deferred failed from the when() group, you could have them pass themselves along with the .reject() call as part of an object literal or something.


[#92958] Thursday, March 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikokorbinm

Total Points: 744
Total Questions: 126
Total Answers: 104

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;