Ticket #9386 (closed feature: wontfix)
Deferreds Feature Request
| Reported by: | alan.spacer@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.7 |
| Component: | deferred | Version: | 1.6.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by rwaldron) (diff)
Wherein a features for jQuery's Deferred objects are proposed:
When you want to take an action for when ALL Deferred objects are resolved or rejected (ie. not unresolved), you have to wrap a secondary Deferred object around each of them, and ensure that these secondary Deferreds are always resolved. This is because in the multiple Deferreds case where one of the Deferreds is rejected, ""jQuery.when"" IMMEDIATELY FIRES the fail callbacks for its master Deferred, even if some of the Deferreds may still be unresolved at that point.
Change History
comment:3 Changed 2 years ago by jaubourg
-1, Start with a plugin, I'll gladly incorporate this in ajaxHooks (which I'd like to have some deferred helpers too)
comment:5 Changed 2 years ago by timmywil
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to deferred
- Description modified (diff)
comment:8 Changed 2 years ago by ajpiano
- Description modified (diff)
+1, I kind of think it's a bug that they don't work this way already.
comment:9 Changed 2 years ago by danheberden
- Description modified (diff)
-1, For fear of deferreds turning into a feature creep pit of despair, use the building blocks to make a plugin
comment:10 Changed 2 years ago by rwaldron
- Keywords 1.7-discuss removed
- Status changed from open to closed
- Resolution set to wontfix
- Description modified (diff)
- Milestone changed from 1.next to 1.7
Plugin first
comment:11 Changed 9 months ago by rg1693@…
Hello, I stumbled upon the same problem. I've written a modified version of $.when to fix it. (it can be tested at http://jsfiddle.net/LTsLJ/ ):
var // Static reference to slice
sliceDeferred = [].slice;
$.notBrokenWhen = function( firstParam ) {
var args = sliceDeferred.call( arguments, 0 ),
i = 0,
length = args.length,
pValues = new Array( length ),
count = length,
pCount = length,
deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ?
firstParam :
jQuery.Deferred(),
promise = deferred.promise(),
rejectedFlag;
function checkAll() {
if (rejectedFlag) {
deferred.rejectWith( deferred, args );
} else {
deferred.resolveWith( deferred, args );
}
}
function resolveFunc( i ) {
return function( value ) {
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
if ( !( --count ) ) {
checkAll();
}
};
}
function progressFunc( i ) {
return function( value ) {
pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
deferred.notifyWith( promise, pValues );
};
}
function rejectFunc( i ) {
return function( value ) {
rejectedFlag = true;
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
if ( !( --count ) ) {
checkAll();
}
}
}
if ( length > 1 ) {
for ( ; i < length; i++ ) {
if ( args[ i ] && args[ i ].promise && jQuery.isFunction( args[ i ].promise ) ) {
args[ i ].promise().then( resolveFunc(i), rejectFunc(i), progressFunc(i) );
} else {
--count;
}
}
if ( !count ) {
deferred.resolveWith( deferred, args );
}
} else if ( deferred !== firstParam ) {
deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
}
return promise;
}
comment:12 Changed 9 months ago by rg1693@…
And I also made a github project: https://github.com/BiAiB/jquery-fullwhen
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Nominating ticket for 1.7 discussion.