// Deferred helper
when: function() {
- var method,
+ var method, resolveContexts,
i = 0,
resolveValues = slice.call( arguments ),
length = resolveValues.length,
// 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 ] );
}
}
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 ),
eventuallyRejected: true,
rejectedStandardPromise: true
},
- willNotify = {
- notified: true,
- eventuallyFulfilled: true,
- eventuallyRejected: true
- },
counter = 49;
QUnit.stop();
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;
} 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 );
} );
} );
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" );