Bug Tracker

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#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:
Blocked by: Blocking:


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')});


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 (3)

comment:1 Changed 4 years ago by timmywil

  • Component changed from unfiled to deferred
  • Owner set to anonymous
  • Priority changed from undecided to low
  • Status changed from new to pending

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.

comment:2 Changed 4 years ago by jaubourg

  • Resolution set to duplicate
  • Status changed from pending to closed

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.

comment:3 Changed 4 years ago by jaubourg

Duplicate of #10836.

Note: See TracTickets for help on using tickets.