Deferred: Make jQuery.when synchronous when possible

Closes gh-3102
Fixes gh-3100
Closes gh-3105
This commit is contained in:
Richard Gibson
2016-05-04 15:30:24 -04:00
parent e8825a529b
commit de71e9755f
2 changed files with 47 additions and 8 deletions

View File

@@ -366,16 +366,21 @@ jQuery.extend( {
// Single- and empty arguments are adopted like Promise.resolve
if ( remaining <= 1 ) {
adoptValue( singleValue, master.resolve, master.reject );
adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject );
// Use .then() to unwrap secondary thenables (cf. gh-3000)
return master.then();
if ( master.state() === "pending" ||
jQuery.isFunction( resolveValues[ i ] && resolveValues[ i ].then ) ) {
return master.then();
}
}
// Multiple arguments are aggregated like Promise.all array elements
while ( i-- ) {
adoptValue( resolveValues[ i ], updateFunc( i ), master.reject );
}
return master.promise();
}
} );