]> source.dussan.org Git - jquery.git/commitdiff
Removes isPending and introduces state as a means to retrieve the Deferred/Promise...
authorjaubourg <j@ubourg.net>
Wed, 12 Oct 2011 00:23:56 +0000 (20:23 -0400)
committerjaubourg <j@ubourg.net>
Wed, 12 Oct 2011 00:23:56 +0000 (20:23 -0400)
src/deferred.js
test/unit/deferred.js

index 9e5f5110a32d5401aae39695aa22bb7a46f73538..457bc521f71d869e9147aafa64de5de45bdd3ab8 100644 (file)
@@ -9,6 +9,7 @@ jQuery.extend({
                var doneList = jQuery.Callbacks( "once memory" ),
                        failList = jQuery.Callbacks( "once memory" ),
                        progressList = jQuery.Callbacks( "memory" ),
+                       state = "pending",
                        lists = {
                                resolve: doneList,
                                reject: failList,
@@ -19,11 +20,13 @@ jQuery.extend({
                                fail: failList.add,
                                progress: progressList.add,
 
+                               state: function() {
+                                       return state;
+                               },
+
+                               // Deprecated
                                isResolved: doneList.fired,
                                isRejected: failList.fired,
-                               isPending: function() {
-                                       return !progressList.locked();
-                               },
 
                                then: function( doneCallbacks, failCallbacks, progressCallbacks ) {
                                        deferred.done( doneCallbacks ).fail( failCallbacks ).progress( progressCallbacks );
@@ -78,9 +81,12 @@ jQuery.extend({
                        deferred[ key + "With" ] = lists[ key ].fireWith;
                }
 
-               // Handle lists exclusiveness
-               deferred.done( failList.disable, progressList.lock )
-                       .fail( doneList.disable, progressList.lock );
+               // Handle state
+               deferred.done( function() {
+                       state = "resolved";
+               }, failList.disable, progressList.lock ).fail( function() {
+                       state = "rejected";
+               }, doneList.disable, progressList.lock );
 
                // Call given func if any
                if ( func ) {
index f8232f9cc650b5be1b13dcd9661339b88ad614fd..9b99ae54194fb7d9970bb5ca4ac759513281fcce 100644 (file)
@@ -8,11 +8,12 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
 
        test("jQuery.Deferred" + withNew, function() {
 
-               expect( 20 );
+               expect( 22 );
 
                createDeferred().resolve().then( function() {
                        ok( true , "Success on resolve" );
                        ok( this.isResolved(), "Deferred is resolved" );
+                       strictEqual( this.state(), "resolved", "Deferred is resolved (state)" );
                }, function() {
                        ok( false , "Error on resolve" );
                }).always( function() {
@@ -24,6 +25,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
                }, function() {
                        ok( true , "Error on reject" );
                        ok( this.isRejected(), "Deferred is rejected" );
+                       strictEqual( this.state(), "rejected", "Deferred is rejected (state)" );
                }).always( function() {
                        ok( true , "Always callback on reject" );
                });
@@ -37,7 +39,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
 
                jQuery.each( "resolve reject".split( " " ), function( _, change ) {
                        createDeferred( function( defer ) {
-                               ok( defer.isPending(), "pending after creation" );
+                               strictEqual( defer.state(), "pending", "pending after creation" );
                                var checked = 0;
                                defer.progress(function( value ) {
                                        strictEqual( value, checked, "Progress: right value (" + value + ") received" );
@@ -45,9 +47,9 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
                                for( checked = 0; checked < 3 ; checked++ ) {
                                        defer.notify( checked );
                                }
-                               ok( defer.isPending(), "pending after notification" );
+                               strictEqual( defer.state(), "pending", "pending after notification" );
                                defer[ change ]();
-                               ok( !defer.isPending(), "not pending after " + change );
+                               notStrictEqual( defer.state(), "pending", "not pending after " + change );
                                defer.notify();
                        });
                });