aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTimmy Willison <timmywillisn@gmail.com>2015-11-11 10:35:37 -0500
committerTimmy Willison <timmywillisn@gmail.com>2015-11-13 11:16:26 -0500
commit78b9eac1198627eb4dad0cc35334c7704449f310 (patch)
tree7a334eee14e995443366927d6f96370344e7b5cc /src
parent8b65446a60c421cfc3411fe273edcc506783f628 (diff)
downloadjquery-78b9eac1198627eb4dad0cc35334c7704449f310.tar.gz
jquery-78b9eac1198627eb4dad0cc35334c7704449f310.zip
Deferred: syncronize single and multiple target handling in $.when
Fixes gh-2546 Fixes gh-2018 Close gh-2707
Diffstat (limited to 'src')
-rw-r--r--src/deferred.js22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/deferred.js b/src/deferred.js
index e1af425d4..846528e79 100644
--- a/src/deferred.js
+++ b/src/deferred.js
@@ -294,19 +294,17 @@ jQuery.extend( {
},
// Deferred helper
- when: function( subordinate /* , ..., subordinateN */ ) {
+ when: function() {
var method,
i = 0,
resolveValues = slice.call( arguments ),
length = resolveValues.length,
// the count of uncompleted subordinates
- remaining = length !== 1 ||
- ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
+ remaining = length,
// the master Deferred.
- // If resolveValues consist of only a single Deferred, just use that.
- master = remaining === 1 ? subordinate : jQuery.Deferred(),
+ master = jQuery.Deferred(),
// Update function for both resolve and progress values
updateFunc = function( i, contexts, values ) {
@@ -316,14 +314,17 @@ jQuery.extend( {
if ( values === progressValues ) {
master.notifyWith( contexts, values );
} else if ( !( --remaining ) ) {
- master.resolveWith( contexts, values );
+ master.resolveWith(
+ contexts.length === 1 ? contexts[ 0 ] : contexts,
+ values
+ );
}
};
},
progressValues, progressContexts, resolveContexts;
// Add listeners to Deferred subordinates; treat others as resolved
- if ( length > 1 ) {
+ if ( length > 0 ) {
progressValues = new Array( length );
progressContexts = new Array( length );
resolveContexts = new Array( length );
@@ -345,14 +346,13 @@ jQuery.extend( {
updateFunc( i, progressContexts, progressValues )
);
} else {
- --remaining;
+ updateFunc( i, resolveContexts, resolveValues )( resolveValues[ i ] );
}
}
- }
// If we're not waiting on anything, resolve the master
- if ( !remaining ) {
- master.resolveWith( resolveContexts, resolveValues );
+ } else {
+ master.resolveWith();
}
return master.promise();