🌐 AI搜索 & 代理 主页
Skip to content

Commit 5d79c64

Browse files
authored
Deferred: Stop inventing jQuery.when() resolution values
Fixes gh-3442 Closes gh-3445
1 parent 1777899 commit 5d79c64

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/deferred.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Thrower( ex ) {
1313
throw ex;
1414
}
1515

16-
function adoptValue( value, resolve, reject ) {
16+
function adoptValue( value, resolve, reject, noValue ) {
1717
var method;
1818

1919
try {
@@ -29,9 +29,10 @@ function adoptValue( value, resolve, reject ) {
2929
// Other non-thenables
3030
} else {
3131

32-
// Support: Android 4.0 only
33-
// Strict mode functions invoked without .call/.apply get global-object context
34-
resolve.call( undefined, value );
32+
// Control `resolve` arguments by letting Array#slice cast boolean `noValue` to integer:
33+
// * false: [ value ].slice( 0 ) => resolve( value )
34+
// * true: [ value ].slice( 1 ) => resolve()
35+
resolve.apply( undefined, [ value ].slice( noValue ) );
3536
}
3637

3738
// For Promises/A+, convert exceptions into rejections
@@ -41,7 +42,7 @@ function adoptValue( value, resolve, reject ) {
4142

4243
// Support: Android 4.0 only
4344
// Strict mode functions invoked without .call/.apply get global-object context
44-
reject.call( undefined, value );
45+
reject.apply( undefined, [ value ] );
4546
}
4647
}
4748

@@ -366,7 +367,8 @@ jQuery.extend( {
366367

367368
// Single- and empty arguments are adopted like Promise.resolve
368369
if ( remaining <= 1 ) {
369-
adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject );
370+
adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject,
371+
!remaining );
370372

371373
// Use .then() to unwrap secondary thenables (cf. gh-3000)
372374
if ( master.state() === "pending" ||

test/unit/deferred.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,11 @@ QUnit.test( "jQuery.when(nonThenable) - like Promise.resolve", function( assert
814814

815815
jQuery.when()
816816
.done( function( resolveValue ) {
817-
assert.strictEqual( resolveValue, undefined, "Resolved .done with no arguments" );
817+
assert.strictEqual( arguments.length, 0, "Resolved .done with no arguments" );
818818
assert.strictEqual( this, defaultContext, "Default .done context with no arguments" );
819819
} )
820820
.then( function( resolveValue ) {
821-
assert.strictEqual( resolveValue, undefined, "Resolved .then with no arguments" );
821+
assert.strictEqual( arguments.length, 0, "Resolved .then with no arguments" );
822822
assert.strictEqual( this, defaultContext, "Default .then context with no arguments" );
823823
} );
824824

0 commit comments

Comments
 (0)