Bug Tracker

Modify

Ticket #11162 (closed bug: duplicate)

Opened 2 years ago

Last modified 2 years ago

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 2 years 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

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 2 years 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.

comment:3 Changed 2 years ago by jaubourg

Duplicate of #10836.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.