]> source.dussan.org Git - jquery.git/commitdiff
Deferred: Stop inventing jQuery.when() resolution values
authorRichard Gibson <richard.gibson@gmail.com>
Fri, 16 Dec 2016 16:45:35 +0000 (11:45 -0500)
committerGitHub <noreply@github.com>
Fri, 16 Dec 2016 16:45:35 +0000 (11:45 -0500)
Fixes gh-3442
Closes gh-3445

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

index 8139515fef7ff942d1fbb170e11ca769b0fe4a47..7e2ced25bed8996631acd2154413963f3df0c77a 100644 (file)
@@ -13,7 +13,7 @@ function Thrower( ex ) {
        throw ex;
 }
 
-function adoptValue( value, resolve, reject ) {
+function adoptValue( value, resolve, reject, noValue ) {
        var method;
 
        try {
@@ -29,9 +29,10 @@ function adoptValue( value, resolve, reject ) {
                // Other non-thenables
                } else {
 
-                       // Support: Android 4.0 only
-                       // Strict mode functions invoked without .call/.apply get global-object context
-                       resolve.call( undefined, value );
+                       // Control `resolve` arguments by letting Array#slice cast boolean `noValue` to integer:
+                       // * false: [ value ].slice( 0 ) => resolve( value )
+                       // * true: [ value ].slice( 1 ) => resolve()
+                       resolve.apply( undefined, [ value ].slice( noValue ) );
                }
 
        // For Promises/A+, convert exceptions into rejections
@@ -41,7 +42,7 @@ function adoptValue( value, resolve, reject ) {
 
                // Support: Android 4.0 only
                // Strict mode functions invoked without .call/.apply get global-object context
-               reject.call( undefined, value );
+               reject.apply( undefined, [ value ] );
        }
 }
 
@@ -366,7 +367,8 @@ jQuery.extend( {
 
                // Single- and empty arguments are adopted like Promise.resolve
                if ( remaining <= 1 ) {
-                       adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject );
+                       adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject,
+                               !remaining );
 
                        // Use .then() to unwrap secondary thenables (cf. gh-3000)
                        if ( master.state() === "pending" ||
index 32f3256814e4399f6b664d8198c02192483c738f..426af4b5f7c92d69be168287c18762710c224a53 100644 (file)
@@ -814,11 +814,11 @@ QUnit.test( "jQuery.when(nonThenable) - like Promise.resolve", function( assert
 
        jQuery.when()
                .done( function( resolveValue ) {
-                       assert.strictEqual( resolveValue, undefined, "Resolved .done with no arguments" );
+                       assert.strictEqual( arguments.length, 0, "Resolved .done with no arguments" );
                        assert.strictEqual( this, defaultContext, "Default .done context with no arguments" );
                } )
                .then( function( resolveValue ) {
-                       assert.strictEqual( resolveValue, undefined, "Resolved .then with no arguments" );
+                       assert.strictEqual( arguments.length, 0, "Resolved .then with no arguments" );
                        assert.strictEqual( this, defaultContext, "Default .then context with no arguments" );
                } );