Ticket #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: | ||
| Blocking: | Blocked by: |
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
comment:1 Changed 20 months ago by rwaldron
- Priority changed from undecided to low
- Resolution set to duplicate
- Status changed from new to closed
- Component changed from unfiled to deferred
comment:5 Changed 20 months 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 4 months 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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
