Bug Tracker

Ticket #9386 (closed feature: wontfix)

Opened 3 years ago

Last modified 2 years ago

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.

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

Change History

comment:1 Changed 3 years ago by john

  • Keywords 1.7-discuss added

Nominating ticket for 1.7 discussion.

comment:2 Changed 3 years ago by rwaldron

  • Description modified (diff)

+0,

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

+0, I like the idea.

comment:5 Changed 3 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:6 Changed 3 years ago by dmethvin

-1, Plugin first?

comment:7 Changed 3 years ago by john

  • Description modified (diff)

+0

comment:8 Changed 3 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 3 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 3 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 2 years 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 2 years ago by rg1693@…

And I also made a github project:  https://github.com/BiAiB/jquery-fullwhen

Note: See TracTickets for help on using tickets.