Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#9386 closed feature (wontfix)

Deferreds Feature Request

Reported by: [email protected] Owned by:
Priority: low Milestone: 1.7
Component: deferred Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by Rick Waldron)

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.

See: http://stackoverflow.com/questions/5824615/

Change History (12)

comment:1 Changed 10 years ago by john

Keywords: 1.7-discuss added

Nominating ticket for 1.7 discussion.

comment:2 Changed 10 years ago by Rick Waldron

Description: modified (diff)

+0,

comment:3 Changed 10 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:4 Changed 10 years ago by timmywil

+0, I like the idea.

comment:5 Changed 10 years ago by timmywil

Component: unfileddeferred
Description: modified (diff)
Priority: undecidedlow
Status: newopen

comment:6 Changed 10 years ago by dmethvin

-1, Plugin first?

comment:7 Changed 10 years ago by john

Description: modified (diff)

+0

comment:8 Changed 10 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 10 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 10 years ago by Rick Waldron

Description: modified (diff)
Keywords: 1.7-discuss removed
Milestone: 1.next1.7
Resolution: wontfix
Status: openclosed

Plugin first

comment:11 Changed 9 years ago by [email protected]

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


Note: See TracTickets for help on using tickets.