Ticket #11162 (closed bug: duplicate)
jQuery.when misbehaves with fail()
| Reported by: | anonymous | Owned by: | anonymous |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | deferred | Version: | 1.7.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
This bug exists in 1.7.1 but dates back at least to 1.5.2.
var deferred1 = $.Deferred();
var deferred2 = $.Deferred();
deferred1.fail(function() { console.info('1'); });
deferred2.fail(function() { console.info('2'); });
$.when(deferred1, deferred2).then(function() { }, function() { console.info('3')});
console.info('ready');
deferred1.reject();
deferred2.reject();
The expected behavior would be to see "1 2 3" in the console. However the actual order is "1 3 2".
It looks like reject() triggers the when().fail() results eagerly. And while this example is a little contrived, this behavior also means that using when().always() for handling partial success doesn't work, because always() gets triggered before all of the requests have resolve()ed or reject()ed.
Change History
comment:1 Changed 16 months ago by timmywil
- Owner set to anonymous
- Priority changed from undecided to low
- Status changed from new to pending
- Component changed from unfiled to deferred
comment:2 Changed 16 months ago by jaubourg
- Status changed from pending to closed
- Resolution set to duplicate
This is not a bug. $.when acts exactly like a logical AND. When one of the operands is false, then the result is false.
What happens here is exactly what you'd expect to happen when you write something like A && B in javascript when A is false: javascript won't even compute the B expression since the result is obligatory false no matter what.
Here, the promise returned by $.when will fail as soon as one of its arguments fails. It's exactly the same logic.
Keep in mind some promises can be pending forever: that way, you get failure as soon as possible (ie. as soon as only one promises fails).
I dunno of any logical operation equivalent for what you propose: that's a definite red flag for me.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket.
Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/. Open the link and click to "Fork" (in the top menu) to get started.