Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#10307 closed enhancement (duplicate)

$.when should accept array of deferreds

Reported by: gondo@… Owned by:
Priority: low Milestone: None
Component: deferred Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:

Description

$.when should accept array of deferreds as parameter. currently, it accepts just 1 deferred object or more arguments (more deferreds separated by comma)

Change History (6)

comment:1 Changed 8 years ago by Rick Waldron

Component: unfileddeferred
Priority: undecidedlow
Resolution: duplicate
Status: newclosed

comment:2 Changed 8 years ago by Rick Waldron

Duplicate of #8256.

comment:4 Changed 8 years ago by ajpiano

#10367 is a duplicate of this ticket.

comment:5 Changed 8 years ago by ajpiano

We don't intend to add this because the values that are passed to jQuery.when are not required to be observable - an array, like anything else besides a deferred or promise, is treated as something that has already been resolved. Another option like introspecting an array to check for the existence of deferreds is overly invasive and brittle, so I believe we'll continue to recommend using $.when.apply(null, arrayOfDeferreds). If you like, you can duckpunch it into your jQuery with something like

var oldWhen = jQuery.when;
jQuery.when = function( arg ) {
  if ( jQuery.isArray( arg ) ) {
    return oldWhen.apply( null, arg );
  } else {
    return oldWhen.apply( Array.prototype.slice.call( arguments ) );
  }
};

comment:6 Changed 7 years ago by anonymous

Not having a way to wait for an array of promises in jQuery is a huge drawback IMHO. In all the time I used $.when in almost all of them I have to use the apply invocation.

Only in the simplest of cases you will store your jqXHR in variables, because doing it that way does not scale for more than 3 or 4 instances and makes for hard to maintian/evolve code.

Both https://github.com/kriskowal/q https://github.com/cujojs/when and https://github.com/kriszyp/node-promise support that syntax, and it is hard for me to see why jQuery does not.

Note: See TracTickets for help on using tickets.