From 2d9d1f006ac98b3dd1f23de1b721f54eb77346db Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Fri, 13 Nov 2015 15:58:03 +0300 Subject: [PATCH] Revert "Deferred: Backwards-compatible standards interoperability" This reverts commit 555a50d340706e3e1e0de09231050493d0ad841e. --- Gruntfile.js | 4 +- package.json | 3 - src/deferred.js | 279 +++--------------- test/index.html | 1 - test/unit/deferred.js | 665 +++++++++--------------------------------- 5 files changed, 170 insertions(+), 782 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 98e446775..43f57a0bf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -207,9 +207,7 @@ module.exports = function( grunt ) { // jQuery on Windows. (see gh-2519) grunt.registerTask( "test_fast", runJsdomTests ? [ "node_smoke_tests" ] : [] ); - grunt.registerTask( "test", [ "test_fast" ].concat( - runJsdomTests ? [ "promises_aplus_tests" ] : [] - ) ); + grunt.registerTask( "test", [ "test_fast" ] ); // Short list as a high frequency watch task grunt.registerTask( "dev", [ "build:*:*", "lint", "uglify", "remove_map_comment", "dist:*" ] ); diff --git a/package.json b/package.json index b48f90a98..fbd98c91d 100644 --- a/package.json +++ b/package.json @@ -41,9 +41,6 @@ "gzip-js": "0.3.2", "jsdom": "5.6.1", "load-grunt-tasks": "1.0.0", - "native-promise-only": "0.7.8-a", - "promises-aplus-tests": "2.1.0", - "q": "1.1.2", "qunitjs": "1.17.1", "qunit-assert-step": "1.0.3", "requirejs": "2.1.17", diff --git a/src/deferred.js b/src/deferred.js index 6c85dd6fd..b56a40104 100644 --- a/src/deferred.js +++ b/src/deferred.js @@ -4,26 +4,14 @@ define( [ "./callbacks" ], function( jQuery, slice ) { -function Identity( v ) { - return v; -} -function Thrower( ex ) { - throw ex; -} - -jQuery.extend( { +jQuery.extend({ Deferred: function( func ) { var tuples = [ - - // action, add listener, callbacks, - // ... .then handlers, argument index, [final state] - [ "notify", "progress", jQuery.Callbacks( "memory" ), - jQuery.Callbacks( "memory" ), 2 ], - [ "resolve", "done", jQuery.Callbacks( "once memory" ), - jQuery.Callbacks( "once memory" ), 0, "resolved" ], - [ "reject", "fail", jQuery.Callbacks( "once memory" ), - jQuery.Callbacks( "once memory" ), 1, "rejected" ] + // action, add listener, listener list, final state + [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ], + [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ], + [ "notify", "progress", jQuery.Callbacks("memory") ] ], state = "pending", promise = { @@ -34,21 +22,13 @@ jQuery.extend( { deferred.done( arguments ).fail( arguments ); return this; }, - - // Keep pipe for back-compat - pipe: function( /* fnDone, fnFail, fnProgress */ ) { + then: function( /* fnDone, fnFail, fnProgress */ ) { var fns = arguments; - - return jQuery.Deferred( function( newDefer ) { + return jQuery.Deferred(function( newDefer ) { jQuery.each( tuples, function( i, tuple ) { - - // Map tuples (progress, done, fail) to arguments (done, fail, progress) - var fn = jQuery.isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ]; - - // deferred.progress(function() { bind to newDefer or newDefer.notify }) - // deferred.done(function() { bind to newDefer or newDefer.resolve }) - // deferred.fail(function() { bind to newDefer or newDefer.reject }) - deferred[ tuple[ 1 ] ]( function() { + var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ]; + // deferred[ done | fail | progress ] for forwarding actions to newDefer + deferred[ tuple[1] ](function() { var returned = fn && fn.apply( this, arguments ); if ( returned && jQuery.isFunction( returned.promise ) ) { returned.promise() @@ -66,162 +46,6 @@ jQuery.extend( { fns = null; } ).promise(); }, - then: function( onFulfilled, onRejected, onProgress ) { - var maxDepth = 0; - function resolve( depth, deferred, handler, special ) { - return function() { - var that = this === promise ? undefined : this, - args = arguments, - mightThrow = function() { - var returned, then; - - // Support: Promises/A+ section 2.3.3.3.3 - // https://promisesaplus.com/#point-59 - // Ignore double-resolution attempts - if ( depth < maxDepth ) { - return; - } - - returned = handler.apply( that, args ); - - // Support: Promises/A+ section 2.3.1 - // https://promisesaplus.com/#point-48 - if ( returned === deferred.promise() ) { - throw new TypeError( "Thenable self-resolution" ); - } - - // Support: Promises/A+ sections 2.3.3.1, 3.5 - // https://promisesaplus.com/#point-54 - // https://promisesaplus.com/#point-75 - // Retrieve `then` only once - then = returned && - - // Support: Promises/A+ section 2.3.4 - // https://promisesaplus.com/#point-64 - // Only check objects and functions for thenability - ( typeof returned === "object" || - typeof returned === "function" ) && - returned.then; - - // Handle a returned thenable - if ( jQuery.isFunction( then ) ) { - - // Special processors (notify) just wait for resolution - if ( special ) { - then.call( - returned, - resolve( maxDepth, deferred, Identity, special ), - resolve( maxDepth, deferred, Thrower, special ) - ); - - // Normal processors (resolve) also hook into progress - } else { - - // ...and disregard older resolution values - maxDepth++; - - then.call( - returned, - resolve( maxDepth, deferred, Identity, special ), - resolve( maxDepth, deferred, Thrower, special ), - resolve( maxDepth, deferred, Identity, - deferred.notify ) - ); - } - - // Handle all other returned values - } else { - - // Only substitue handlers pass on context - // and multiple values (non-spec behavior) - if ( handler !== Identity ) { - that = undefined; - args = [ returned ]; - } - - // Process the value(s) - // Default process is resolve - ( special || deferred.resolveWith )( - that || deferred.promise(), args ); - } - }, - - // Only normal processors (resolve) catch and reject exceptions - process = special ? - mightThrow : - function() { - try { - mightThrow(); - } catch ( e ) { - - // Support: Promises/A+ section 2.3.3.3.4.1 - // https://promisesaplus.com/#point-61 - // Ignore post-resolution exceptions - if ( depth + 1 >= maxDepth ) { - - // Only substitue handlers pass on context - // and multiple values (non-spec behavior) - if ( handler !== Thrower ) { - that = undefined; - args = [ e ]; - } - - deferred.rejectWith( that || deferred.promise(), - args ); - } - } - }; - - // Support: Promises/A+ section 2.3.3.3.1 - // https://promisesaplus.com/#point-57 - // Re-resolve promises immediately to dodge false rejection from - // subsequent errors - if ( depth ) { - process(); - } else { - window.setTimeout( process ); - } - }; - } - - return jQuery.Deferred( function( newDefer ) { - - // progress_handlers.add( ... ) - tuples[ 0 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onProgress ) ? - onProgress : - Identity, - newDefer.notifyWith - ) - ); - - // fulfilled_handlers.add( ... ) - tuples[ 1 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onFulfilled ) ? - onFulfilled : - Identity - ) - ); - - // rejected_handlers.add( ... ) - tuples[ 2 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onRejected ) ? - onRejected : - Thrower - ) - ); - } ).promise(); - }, - // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function( obj ) { @@ -230,54 +54,34 @@ jQuery.extend( { }, deferred = {}; + // Keep pipe for back-compat + promise.pipe = promise.then; + // Add list-specific methods jQuery.each( tuples, function( i, tuple ) { var list = tuple[ 2 ], - stateString = tuple[ 5 ]; + stateString = tuple[ 3 ]; - // promise.progress = list.add - // promise.done = list.add - // promise.fail = list.add - promise[ tuple[ 1 ] ] = list.add; + // promise[ done | fail | progress ] = list.add + promise[ tuple[1] ] = list.add; // Handle state if ( stateString ) { - list.add( - function() { - - // state = "resolved" (i.e., fulfilled) - // state = "rejected" - state = stateString; - }, + list.add(function() { + // state = [ resolved | rejected ] + state = stateString; - // rejected_callbacks.disable - // fulfilled_callbacks.disable - tuples[ 3 - i ][ 2 ].disable, - - // progress_callbacks.lock - tuples[ 0 ][ 2 ].lock - ); + // [ reject_list | resolve_list ].disable; progress_list.lock + }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock ); } - // progress_handlers.fire - // fulfilled_handlers.fire - // rejected_handlers.fire - list.add( tuple[ 3 ].fire ); - - // deferred.notify = function() { deferred.notifyWith(...) } - // deferred.resolve = function() { deferred.resolveWith(...) } - // deferred.reject = function() { deferred.rejectWith(...) } - deferred[ tuple[ 0 ] ] = function() { - deferred[ tuple[ 0 ] + "With" ]( this === deferred ? promise : this, arguments ); + // deferred[ resolve | reject | notify ] + deferred[ tuple[0] ] = function() { + deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments ); return this; }; - - // deferred.notifyWith = list.fireWith - // deferred.resolveWith = list.fireWith - // deferred.rejectWith = list.fireWith - deferred[ tuple[ 0 ] + "With" ] = list.fireWith; - } ); - + deferred[ tuple[0] + "With" ] = list.fireWith; + }); // Make the deferred a promise promise.promise( deferred ); @@ -292,8 +96,7 @@ jQuery.extend( { // Deferred helper when: function( subordinate /* , ..., subordinateN */ ) { - var method, - i = 0, + var i = 0, resolveValues = slice.call( arguments ), length = resolveValues.length, @@ -303,7 +106,7 @@ jQuery.extend( { // the master Deferred. // If resolveValues consist of only a single Deferred, just use that. - master = remaining === 1 ? subordinate : jQuery.Deferred(), + deferred = remaining === 1 ? subordinate : jQuery.Deferred(), // Update function for both resolve and progress values updateFunc = function( i, contexts, values ) { @@ -311,12 +114,13 @@ jQuery.extend( { contexts[ i ] = this; values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value; if ( values === progressValues ) { - master.notifyWith( contexts, values ); + deferred.notifyWith( contexts, values ); } else if ( !( --remaining ) ) { - master.resolveWith( contexts, values ); + deferred.resolveWith( contexts, values ); } }; }, + progressValues, progressContexts, resolveContexts; // Add listeners to Deferred subordinates; treat others as resolved @@ -325,22 +129,11 @@ jQuery.extend( { progressContexts = new Array( length ); resolveContexts = new Array( length ); for ( ; i < length; i++ ) { - if ( resolveValues[ i ] && - jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) { - - method.call( resolveValues[ i ] ) + if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) { + resolveValues[ i ].promise() .progress( updateFunc( i, progressContexts, progressValues ) ) .done( updateFunc( i, resolveContexts, resolveValues ) ) - .fail( master.reject ); - } else if ( resolveValues[ i ] && - jQuery.isFunction( ( method = resolveValues[ i ].then ) ) ) { - - method.call( - resolveValues[ i ], - updateFunc( i, resolveContexts, resolveValues ), - master.reject, - updateFunc( i, progressContexts, progressValues ) - ); + .fail( deferred.reject ); } else { --remaining; } @@ -349,10 +142,10 @@ jQuery.extend( { // If we're not waiting on anything, resolve the master if ( !remaining ) { - master.resolveWith( resolveContexts, resolveValues ); + deferred.resolveWith( resolveContexts, resolveValues ); } - return master.promise(); + return deferred.promise(); } } ); diff --git a/test/index.html b/test/index.html index 54a1ee041..dfb574137 100644 --- a/test/index.html +++ b/test/index.html @@ -15,7 +15,6 @@ - diff --git a/test/unit/deferred.js b/test/unit/deferred.js index a71273727..e050f0454 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -14,34 +14,34 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) { var defer = createDeferred(); - assert.ok( jQuery.isFunction( defer.pipe ), "defer.pipe is a function" ); + assert.strictEqual( defer.pipe, defer.then, "pipe is an alias of then" ); - createDeferred().resolve().done( function() { + createDeferred().resolve().done(function() { assert.ok( true, "Success on resolve" ); assert.strictEqual( this.state(), "resolved", "Deferred is resolved (state)" ); - } ).fail( function() { + }).fail(function() { assert.ok( false, "Error on resolve" ); - } ).always( function() { + }).always(function() { assert.ok( true, "Always callback on resolve" ); - } ); + }); - createDeferred().reject().done( function() { + createDeferred().reject().done(function() { assert.ok( false, "Success on reject" ); - } ).fail( function() { + }).fail(function() { assert.ok( true, "Error on reject" ); assert.strictEqual( this.state(), "rejected", "Deferred is rejected (state)" ); - } ).always( function() { + }).always(function() { assert.ok( true, "Always callback on reject" ); - } ); + }); - createDeferred( function( defer ) { + createDeferred(function( defer ) { assert.ok( this === defer, "Defer passed as this & first argument" ); - this.resolve( "done" ); - } ).done( function( value ) { + this.resolve("done"); + }).done(function( value ) { assert.strictEqual( value, "done", "Passed function executed" ); - } ); + }); - createDeferred( function( defer ) { + createDeferred(function( defer ) { var promise = defer.promise(), func = function() {}, funcPromise = defer.promise( func ); @@ -93,126 +93,79 @@ QUnit.test( "jQuery.Deferred - chainability", function( assert ) { } ); QUnit.test( "jQuery.Deferred.then - filtering (done)", function( assert ) { - assert.expect( 4 ); var value1, value2, value3, defer = jQuery.Deferred(), - piped = defer.then( function( a, b ) { + piped = defer.then(function( a, b ) { return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); + }); - piped.done( function( result ) { + piped.done(function( result ) { value3 = result; - } ); + }); - defer.done( function( a, b ) { + defer.done(function( a, b ) { value1 = a; value2 = b; - } ); + }); - defer.resolve( 2, 3 ).then( function() { - assert.strictEqual( value1, 2, "first resolve value ok" ); - assert.strictEqual( value2, 3, "second resolve value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); + defer.resolve( 2, 3 ); - jQuery.Deferred().reject().then( function() { + assert.strictEqual( value1, 2, "first resolve value ok" ); + assert.strictEqual( value2, 3, "second resolve value ok" ); + assert.strictEqual( value3, 6, "result of filter ok" ); + + jQuery.Deferred().reject().then(function() { assert.ok( false, "then should not be called on reject" ); - } ).then( null, done.pop() ); + }); - jQuery.Deferred().resolve().then( jQuery.noop ).done( function( value ) { + jQuery.Deferred().resolve().then( jQuery.noop ).done(function( value ) { assert.strictEqual( value, undefined, "then done callback can return undefined/null" ); - done.pop().call(); - } ); + }); } ); QUnit.test( "jQuery.Deferred.then - filtering (fail)", function( assert ) { - assert.expect( 4 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, function( a, b ) { return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); + }); - piped.done( function( result ) { + piped.fail(function( result ) { value3 = result; - } ); + }); - defer.fail( function( a, b ) { + defer.fail(function( a, b ) { value1 = a; value2 = b; - } ); - - defer.reject( 2, 3 ).then( null, function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve().then( null, function() { - assert.ok( false, "then should not be called on resolve" ); - } ).then( done.pop() ); - - jQuery.Deferred().reject().then( null, jQuery.noop ).done( function( value ) { - assert.strictEqual( value, undefined, "then fail callback can return undefined/null" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - filtering (fail)", function( assert ) { - - assert.expect( 4 ); + }); - var value1, value2, value3, - defer = jQuery.Deferred(), - piped = defer.pipe( null, function( a, b ) { - return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); - - piped.fail( function( result ) { - value3 = result; - } ); - - defer.fail( function( a, b ) { - value1 = a; - value2 = b; - } ); + defer.reject( 2, 3 ); - defer.reject( 2, 3 ).pipe( null, function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); + strictEqual( value1, 2, "first reject value ok" ); + strictEqual( value2, 3, "second reject value ok" ); + strictEqual( value3, 6, "result of filter ok" ); - jQuery.Deferred().resolve().pipe( null, function() { - assert.ok( false, "then should not be called on resolve" ); - } ).then( done.pop() ); + jQuery.Deferred().resolve().then( null, function() { + ok( false, "then should not be called on resolve" ); + }); - jQuery.Deferred().reject().pipe( null, jQuery.noop ).fail( function( value ) { - assert.strictEqual( value, undefined, "then fail callback can return undefined/null" ); - done.pop().call(); - } ); -} ); + jQuery.Deferred().reject().then( null, jQuery.noop ).fail(function( value ) { + strictEqual( value, undefined, "then fail callback can return undefined/null" ); + }); +}); QUnit.test( "jQuery.Deferred.then - filtering (progress)", function( assert ) { - assert.expect( 3 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, null, function( a, b ) { return a * b; - } ), - done = assert.async(); + } ); piped.progress( function( result ) { value3 = result; @@ -223,16 +176,14 @@ QUnit.test( "jQuery.Deferred.then - filtering (progress)", function( assert ) { value2 = b; } ); - defer.notify( 2, 3 ).then( null, null, function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + defer.notify( 2, 3 ); -QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) { + assert.strictEqual( value1, 2, "first progress value ok" ); + assert.strictEqual( value2, 3, "second progress value ok" ); + assert.strictEqual( value3, 6, "result of filter ok" ); +}); +QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) { assert.expect( 3 ); var value1, value2, value3, @@ -241,8 +192,7 @@ QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) { return jQuery.Deferred( function( defer ) { defer.reject( a * b ); } ); - } ), - done = assert.async(); + } ); piped.fail( function( result ) { value3 = result; @@ -255,16 +205,12 @@ QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) { defer.resolve( 2, 3 ); - piped.fail( function() { - assert.strictEqual( value1, 2, "first resolve value ok" ); - assert.strictEqual( value2, 3, "second resolve value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + assert.strictEqual( value1, 2, "first resolve value ok" ); + assert.strictEqual( value2, 3, "second resolve value ok" ); + assert.strictEqual( value3, 6, "result of filter ok" ); +}); QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) { - assert.expect( 3 ); var value1, value2, value3, @@ -273,8 +219,7 @@ QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) { return jQuery.Deferred( function( defer ) { defer.resolve( a * b ); } ); - } ), - done = assert.async(); + } ); piped.done( function( result ) { value3 = result; @@ -287,16 +232,12 @@ QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) { defer.reject( 2, 3 ); - piped.done( function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + assert.strictEqual( value1, 2, "first reject value ok" ); + assert.strictEqual( value2, 3, "second reject value ok" ); + assert.strictEqual( value3, 6, "result of filter ok" ); +}); QUnit.test( "jQuery.Deferred.then - deferred (progress)", function( assert ) { - assert.expect( 3 ); var value1, value2, value3, @@ -304,50 +245,8 @@ QUnit.test( "jQuery.Deferred.then - deferred (progress)", function( assert ) { piped = defer.then( null, null, function( a, b ) { return jQuery.Deferred( function( defer ) { defer.resolve( a * b ); - } ); - } ), - done = assert.async(); - - piped.progress( function( result ) { - return jQuery.Deferred().resolve().then( function() { - return result; - } ).then( function( result ) { - value3 = result; - } ); - } ); - - defer.progress( function( a, b ) { - value1 = a; - value2 = b; - } ); - - defer.notify( 2, 3 ); - - piped.then( null, null, function( result ) { - return jQuery.Deferred().resolve().then( function() { - return result; - } ).then( function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); + } ); } ); - } ); -} ); - -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - deferred (progress)", function( assert ) { - - assert.expect( 3 ); - - var value1, value2, value3, - defer = jQuery.Deferred(), - piped = defer.pipe( null, null, function( a, b ) { - return jQuery.Deferred( function( defer ) { - defer.resolve( a * b ); - } ); - } ), - done = assert.async(); - piped.done( function( result ) { value3 = result; } ); @@ -359,37 +258,29 @@ QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - deferred (progress)", function( defer.notify( 2, 3 ); - piped.done( function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + assert.strictEqual( value1, 2, "first progress value ok" ); + assert.strictEqual( value2, 3, "second progress value ok" ); + assert.strictEqual( value3, 6, "result of filter ok" ); +}); QUnit.test( "jQuery.Deferred.then - context", function( assert ) { - assert.expect( 7 ); var defer, piped, defer2, piped2, - context = {}, - done = jQuery.map( new Array( 4 ), function() { return assert.async(); } ); + context = {}; jQuery.Deferred().resolveWith( context, [ 2 ] ).then( function( value ) { return value * 3; - } ).done( function( value ) { - assert.notStrictEqual( this, context, "custom context not propagated through .then" ); + } ).done(function( value ) { + assert.strictEqual( this, context, "custom context correctly propagated" ); assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); + }); - jQuery.Deferred().resolve().then( function() { - return jQuery.Deferred().resolveWith( context ); - } ).done( function() { - assert.strictEqual( this, context, - "custom context of returned deferred correctly propagated" ); - done.pop().call(); - } ); + jQuery.Deferred().resolve().then(function() { + return jQuery.Deferred().resolveWith(context); + }).done(function() { + assert.strictEqual( this, context, "custom context of returned deferred correctly propagated" ); + }); defer = jQuery.Deferred(); piped = defer.then( function( value ) { @@ -398,229 +289,25 @@ QUnit.test( "jQuery.Deferred.then - context", function( assert ) { defer.resolve( 2 ); - piped.done( function( value ) { - assert.strictEqual( this, piped, - "default context gets updated to latest promise in the chain" ); + piped.done(function( value ) { + assert.strictEqual( this, piped, "default context gets updated to latest promise in the chain" ); assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - + }); defer2 = jQuery.Deferred(); piped2 = defer2.then(); defer2.resolve( 2 ); - piped2.done( function( value ) { - assert.strictEqual( this, piped2, - "default context updated to latest promise in the chain (without passing function)" ); - assert.strictEqual( value, 2, "proper value received (without passing function)" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - context", function( assert ) { - - assert.expect( 7 ); - - var defer, piped, defer2, piped2, - context = {}, - done = jQuery.map( new Array( 4 ), function() { return assert.async(); } ); - - jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe( function( value ) { - return value * 3; - } ).done( function( value ) { - assert.strictEqual( this, context, "[PIPE ONLY] custom context correctly propagated" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve().pipe( function() { - return jQuery.Deferred().resolveWith( context ); - } ).done( function() { - assert.strictEqual( this, context, - "custom context of returned deferred correctly propagated" ); - done.pop().call(); - } ); - - defer = jQuery.Deferred(); - piped = defer.pipe( function( value ) { - return value * 3; - } ); - - defer.resolve( 2 ); - - piped.done( function( value ) { - assert.strictEqual( this, piped, - "default context gets updated to latest promise in the chain" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - - defer2 = jQuery.Deferred(); - piped2 = defer2.pipe(); - - defer2.resolve( 2 ); - - piped2.done( function( value ) { - assert.strictEqual( this, piped2, - "default context updated to latest promise in the chain (without passing function)" ); + piped2.done(function( value ) { + assert.strictEqual( this, piped2, "default context gets updated to latest promise in the chain (without passing function)" ); assert.strictEqual( value, 2, "proper value received (without passing function)" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) { - - assert.expect( 1 ); - - var done = assert.async(); - - var defer = jQuery.Deferred().done( function() { - setTimeout( done ); - throw new Error(); - } ); - - defer.then( function() { - assert.ok( true, "errors in .done callbacks don't stop .then handlers" ); - } ); - - try { - defer.resolve(); - } catch ( _ ) {} -} ); - -QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) { - - assert.expect( 8 ); - - var context = { id: "callback context" }, - thenable = jQuery.Deferred().resolve( "thenable fulfillment" ).promise(), - done = jQuery.map( new Array( 8 ), function() { return assert.async(); } ); - - thenable.unwrapped = false; - - jQuery.Deferred().resolve( 1, 2 ).then( function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then fulfillment callbacks receive all resolution values" ); - done.pop().call(); - } ); - jQuery.Deferred().reject( 1, 2 ).then( null, function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then rejection callbacks receive all rejection values" ); - done.pop().call(); - } ); - jQuery.Deferred().notify( 1, 2 ).then( null, null, function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then progress callbacks receive all progress values" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolveWith( context ).then( function() { - assert.deepEqual( this, context, ".then fulfillment callbacks receive context" ); - done.pop().call(); - } ); - jQuery.Deferred().rejectWith( context ).then( null, function() { - assert.deepEqual( this, context, ".then rejection callbacks receive context" ); - done.pop().call(); - } ); - jQuery.Deferred().notifyWith( context ).then( null, null, function() { - assert.deepEqual( this, context, ".then progress callbacks receive context" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve( thenable ).done( function( value ) { - assert.strictEqual( value, thenable, ".done doesn't unwrap thenables" ); - done.pop().call(); - } ); - - jQuery.Deferred().notify( thenable ).then().then( null, null, function( value ) { - assert.strictEqual( value, "thenable fulfillment", - ".then implicit progress callbacks unwrap thenables" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "jQuery.Deferred.then - progress and thenables", function( assert ) { - - assert.expect( 2 ); - - var trigger = jQuery.Deferred().notify(), - expectedProgress = [ "baz", "baz" ], - done = jQuery.map( new Array( 2 ), function() { return assert.async(); } ), - failer = function( evt ) { - return function() { - assert.ok( false, "no unexpected " + evt ); - }; - }; - - trigger.then( null, null, function() { - var notifier = jQuery.Deferred().notify( "foo" ); - setTimeout( function() { - notifier.notify( "bar" ).resolve( "baz" ); - } ); - return notifier; - } ).then( failer( "fulfill" ), failer( "reject" ), function( v ) { - assert.strictEqual( v, expectedProgress.shift(), "expected progress value" ); - done.pop().call(); - } ); - trigger.notify(); -} ); - -QUnit.test( "jQuery.Deferred - notify and resolve", function( assert ) { - - assert.expect( 7 ); - - var notifiedResolved = jQuery.Deferred().notify( "foo" )/*xxx .resolve( "bar" )*/, - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); - - notifiedResolved.progress( function( v ) { - assert.strictEqual( v, "foo", "progress value" ); - } ); - - notifiedResolved.pipe().progress( function( v ) { - assert.strictEqual( v, "foo", "piped progress value" ); - } ); - - notifiedResolved.pipe( null, null, function() { - return "baz"; - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "replaced piped progress value" ); - } ); - - notifiedResolved.pipe( null, null, function() { - return jQuery.Deferred().notify( "baz" ).resolve( "quux" ); - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "deferred replaced piped progress value" ); - } ); - - notifiedResolved.then().progress( function( v ) { - assert.strictEqual( v, "foo", "then'd progress value" ); - done.pop().call(); - } ); - - notifiedResolved.then( null, null, function() { - return "baz"; - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "replaced then'd progress value" ); - done.pop().call(); - } ); - - notifiedResolved.then( null, null, function() { - return jQuery.Deferred().notify( "baz" ).resolve( "quux" ); - } ).progress( function( v ) { - - // Progress from the surrogate deferred is ignored - assert.strictEqual( v, "quux", "deferred replaced then'd progress value" ); - done.pop().call(); - } ); -} ); + }); +}); QUnit.test( "jQuery.when", function( assert ) { - assert.expect( 37 ); - // Some other objects - jQuery.each( { + jQuery.each({ "an empty string": "", "a non-empty string": "some string", "zero": 0, @@ -635,21 +322,21 @@ QUnit.test( "jQuery.when", function( assert ) { }, function( message, value ) { assert.ok( jQuery.isFunction( - jQuery.when( value ).done( function( resolveValue ) { + jQuery.when( value ).done(function( resolveValue ) { assert.strictEqual( this, window, "Context is the global object with " + message ); assert.strictEqual( resolveValue, value, "Test the promise was resolved with " + message ); - } ).promise + }).promise ), "Test " + message + " triggers the creation of a new Promise" ); - } ); + }); assert.ok( jQuery.isFunction( - jQuery.when().done( function( resolveValue ) { + jQuery.when().done(function( resolveValue ) { assert.strictEqual( this, window, "Test the promise was resolved with window as its context" ); assert.strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" ); - } ).promise + }).promise ), "Test calling when with no parameter triggers the creation of a new Promise" ); @@ -657,63 +344,49 @@ QUnit.test( "jQuery.when", function( assert ) { var cache, context = {}; - jQuery.when( jQuery.Deferred().resolveWith( context ) ).done( function() { + jQuery.when( jQuery.Deferred().resolveWith( context ) ).done(function() { assert.strictEqual( this, context, "when( promise ) propagates context" ); - } ); + }); - jQuery.each( [ 1, 2, 3 ], function( k, i ) { + jQuery.each([ 1, 2, 3 ], function( k, i ) { - jQuery.when( cache || jQuery.Deferred( function() { + jQuery.when( cache || jQuery.Deferred(function() { this.resolve( i ); - } ) - ).done( function( value ) { + }) + ).done(function( value ) { assert.strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) ); cache = value; - } ); + }); - } ); + }); } ); QUnit.test( "jQuery.when - joined", function( assert ) { - - assert.expect( 195 ); + assert.expect( 119 ); 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 ), - rejectedStandardPromise: Promise.reject( 0 ) + value: 1, + success: jQuery.Deferred().resolve( 1 ), + error: jQuery.Deferred().reject( 0 ), + futureSuccess: jQuery.Deferred().notify( true ), + futureError: jQuery.Deferred().notify( true ), + notify: jQuery.Deferred().notify( true ) }, willSucceed = { - rawValue: true, - fulfilled: true, - eventuallyFulfilled: true, - fulfilledStandardPromise: true + value: true, + success: true, + futureSuccess: true }, willError = { - rejected: true, - eventuallyRejected: true, - rejectedStandardPromise: true + error: true, + futureError: true }, willNotify = { - notified: true, - eventuallyFulfilled: true, - eventuallyRejected: true - }, - counter = 49; - - QUnit.stop(); - - function restart() { - if ( !--counter ) { - QUnit.start(); - } - } + futureSuccess: true, + futureError: true, + notify: true + }; jQuery.each( deferreds, function( id1, defer1 ) { jQuery.each( deferreds, function( id2, defer2 ) { @@ -722,125 +395,53 @@ QUnit.test( "jQuery.when - joined", function( assert ) { 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() : - ( defer1.then ? window : undefined ), - context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : - ( defer2.then ? window : undefined ); + code = id1 + "/" + id2, + context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : undefined, + context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : undefined; - jQuery.when( defer1, defer2 ).done( function( a, b ) { + jQuery.when( defer1, defer2 ).done(function( a, b ) { if ( shouldResolve ) { assert.deepEqual( [ a, b ], expected, code + " => resolve" ); assert.strictEqual( this[ 0 ], context1, code + " => first context OK" ); assert.strictEqual( this[ 1 ], context2, code + " => second context OK" ); } else { - assert.ok( false, code + " => resolve" ); + ok( false, code + " => resolve" ); } - } ).fail( function( a, b ) { + }).fail(function( a, b ) { if ( shouldError ) { assert.deepEqual( [ a, b ], expected, code + " => reject" ); } else { - assert.ok( false, code + " => reject" ); + ok( false, code + " => reject" ); } - } ).progress( function( a, b ) { + }).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.eventuallyFulfilled.resolve( 1 ); - deferreds.eventuallyRejected.reject( 0 ); -} ); + }); + }); + }); -QUnit.test( "jQuery.when - resolved", function( assert ) { + deferreds.futureSuccess.resolve( 1 ); + deferreds.futureError.reject( 0 ); +}); +QUnit.test( "jQuery.when - resolved", function( assert ) { assert.expect( 6 ); 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 ) { + 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 ) { + }).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" ); - } ).fail( function() { + }).fail(function() { assert.ok( false, "Error on resolve" ); - } ); -} ); - -QUnit.test( "jQuery.when - filtering", function( assert ) { + }); - assert.expect( 2 ); - - function increment( x ) { - return x + 1; - } - - QUnit.stop(); - - jQuery.when( - jQuery.Deferred().resolve( 3 ).then( increment ), - jQuery.Deferred().reject( 5 ).then( null, increment ) - ).done( function( four, six ) { - assert.strictEqual( four, 4, "resolved value incremented" ); - assert.strictEqual( six, 6, "rejected value incremented" ); - QUnit.start(); - } ); -} ); - -QUnit.test( "jQuery.when - exceptions", function( assert ) { - - assert.expect( 2 ); - - function woops() { - throw "exception thrown"; - } - - QUnit.stop(); - - jQuery.Deferred().resolve().then( woops ).fail( function( doneException ) { - assert.strictEqual( doneException, "exception thrown", "throwing in done handler" ); - jQuery.Deferred().reject().then( null, woops ).fail( function( failException ) { - assert.strictEqual( failException, "exception thrown", "throwing in fail handler" ); - QUnit.start(); - } ); - } ); -} ); - -QUnit.test( "jQuery.when - chaining", function( assert ) { - - assert.expect( 4 ); - - var defer = jQuery.Deferred(); - - function chain() { - return defer; - } - - function chainStandard() { - return Promise.resolve( "std deferred" ); - } - - QUnit.stop(); - - jQuery.when( - jQuery.Deferred().resolve( 3 ).then( chain ), - jQuery.Deferred().reject( 5 ).then( null, chain ), - jQuery.Deferred().resolve( 3 ).then( chainStandard ), - jQuery.Deferred().reject( 5 ).then( null, chainStandard ) - ).done( function( v1, v2, s1, s2 ) { - assert.strictEqual( v1, "other deferred", "chaining in done handler" ); - assert.strictEqual( v2, "other deferred", "chaining in fail handler" ); - assert.strictEqual( s1, "std deferred", "chaining thenable in done handler" ); - assert.strictEqual( s2, "std deferred", "chaining thenable in fail handler" ); - QUnit.start(); - } ); - - defer.resolve( "other deferred" ); -} ); +}); -- 2.39.5