]> source.dussan.org Git - jquery.git/commitdiff
Deferred: Remove undocumented progress notifications in $.when
authorDave Methvin <dave.methvin@gmail.com>
Tue, 12 Jan 2016 00:10:28 +0000 (19:10 -0500)
committerDave Methvin <dave.methvin@gmail.com>
Wed, 13 Jan 2016 17:21:17 +0000 (12:21 -0500)
Fixes gh-2710
Closes gh-2816

src/deferred.js
test/unit/deferred.js

index 846528e79581a558c5edb5276b9d7f0cd51c9ec5..ff12ac3e0cf2efe063c26c9c7f53de1a759ea296 100644 (file)
@@ -295,7 +295,7 @@ jQuery.extend( {
 
        // Deferred helper
        when: function() {
-               var method,
+               var method, resolveContexts,
                        i = 0,
                        resolveValues = slice.call( arguments ),
                        length = resolveValues.length,
@@ -306,47 +306,44 @@ jQuery.extend( {
                        // the master Deferred.
                        master = jQuery.Deferred(),
 
-                       // Update function for both resolve and progress values
-                       updateFunc = function( i, contexts, values ) {
+                       // Update function for both resolving subordinates
+                       updateFunc = function( i ) {
                                return function( value ) {
-                                       contexts[ i ] = this;
-                                       values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
-                                       if ( values === progressValues ) {
-                                               master.notifyWith( contexts, values );
-                                       } else if ( !( --remaining ) ) {
+                                       resolveContexts[ i ] = this;
+                                       resolveValues[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
+                                       if ( !( --remaining ) ) {
                                                master.resolveWith(
-                                                       contexts.length === 1 ? contexts[ 0 ] : contexts,
-                                                       values
+                                                       resolveContexts.length === 1 ? resolveContexts[ 0 ] : resolveContexts,
+                                                       resolveValues
                                                );
                                        }
                                };
-                       },
-                       progressValues, progressContexts, resolveContexts;
+                       };
 
-               // Add listeners to Deferred subordinates; treat others as resolved
+               // Add listeners to promise-like subordinates; treat others as resolved
                if ( length > 0 ) {
-                       progressValues = new Array( length );
-                       progressContexts = new Array( length );
                        resolveContexts = new Array( length );
                        for ( ; i < length; i++ ) {
+
+                               // jQuery.Deferred - treated specially to get resolve-sync behavior
                                if ( resolveValues[ i ] &&
                                        jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) {
 
                                        method.call( resolveValues[ i ] )
-                                               .progress( updateFunc( i, progressContexts, progressValues ) )
-                                               .done( updateFunc( i, resolveContexts, resolveValues ) )
+                                               .done( updateFunc( i ) )
                                                .fail( master.reject );
+
+                               // Other thenables
                                } else if ( resolveValues[ i ] &&
                                        jQuery.isFunction( ( method = resolveValues[ i ].then ) ) ) {
 
                                        method.call(
                                                resolveValues[ i ],
-                                               updateFunc( i, resolveContexts, resolveValues ),
-                                               master.reject,
-                                               updateFunc( i, progressContexts, progressValues )
+                                               updateFunc( i ),
+                                               master.reject
                                        );
                                } else {
-                                       updateFunc( i, resolveContexts, resolveValues )( resolveValues[ i ] );
+                                       updateFunc( i )( resolveValues[ i ] );
                                }
                        }
 
index 1a721d1ee003c0b7ed2f2a465664d5f05d2e1ac9..83c2f4f6bd4c190b61dcd8f7c0f6ad73b9db5df1 100644 (file)
@@ -710,13 +710,12 @@ QUnit.test( "jQuery.when", function( assert ) {
 
 QUnit.test( "jQuery.when - joined", function( assert ) {
 
-       assert.expect( 195 );
+       assert.expect( 81 );
 
        var deferreds = {
                        rawValue: 1,
                        fulfilled: jQuery.Deferred().resolve( 1 ),
                        rejected: jQuery.Deferred().reject( 0 ),
-                       notified: jQuery.Deferred().notify( true ),
                        eventuallyFulfilled: jQuery.Deferred().notify( true ),
                        eventuallyRejected: jQuery.Deferred().notify( true ),
                        fulfilledStandardPromise: Promise.resolve( 1 ),
@@ -733,11 +732,6 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
                        eventuallyRejected: true,
                        rejectedStandardPromise: true
                },
-               willNotify = {
-                       notified: true,
-                       eventuallyFulfilled: true,
-                       eventuallyRejected: true
-               },
                counter = 49;
 
        QUnit.stop();
@@ -752,9 +746,7 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
                jQuery.each( deferreds, function( id2, defer2 ) {
                        var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
                                shouldError = willError[ id1 ] || willError[ id2 ],
-                               shouldNotify = willNotify[ id1 ] || willNotify[ id2 ],
                                expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
-                               expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
                                code = "jQuery.when( " + id1 + ", " + id2 + " )",
                                context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : window,
                                context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : window;
@@ -773,10 +765,6 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
                                } else {
                                        assert.ok( false, code + " => reject" );
                                }
-                       } ).progress( function( a, b ) {
-                               assert.deepEqual( [ a, b ], expectedNotify, code + " => progress" );
-                               assert.strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" );
-                               assert.strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" );
                        } ).always( restart );
                } );
        } );
@@ -784,19 +772,15 @@ QUnit.test( "jQuery.when - joined", function( assert ) {
        deferreds.eventuallyRejected.reject( 0 );
 } );
 
-QUnit.test( "jQuery.when - resolved", function( assert ) {
+QUnit.test( "jQuery.when - notify does not affect resolved", function( assert ) {
 
-       assert.expect( 6 );
+       assert.expect( 3 );
 
        var a = jQuery.Deferred().notify( 1 ).resolve( 4 ),
                b = jQuery.Deferred().notify( 2 ).resolve( 5 ),
                c = jQuery.Deferred().notify( 3 ).resolve( 6 );
 
-       jQuery.when( a, b, c ).progress( function( a, b, c ) {
-               assert.strictEqual( a, 1, "first notify value ok" );
-               assert.strictEqual( b, 2, "second notify value ok" );
-               assert.strictEqual( c, 3, "third notify value ok" );
-       } ).done( function( a, b, c ) {
+       jQuery.when( a, b, c ).done( function( a, b, c ) {
                assert.strictEqual( a, 4, "first resolve value ok" );
                assert.strictEqual( b, 5, "second resolve value ok" );
                assert.strictEqual( c, 6, "third resolve value ok" );