]> source.dussan.org Git - jquery.git/commitdiff
Deferred: add .catch handler 2451/head
authorTimmy Willison <timmywillisn@gmail.com>
Mon, 6 Jul 2015 14:13:01 +0000 (10:13 -0400)
committerTimmy Willison <timmywillisn@gmail.com>
Mon, 6 Jul 2015 14:47:30 +0000 (10:47 -0400)
Fixes gh-2102

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

index 188303c9759137a7340e365c9f5300e1c91e9281..216baabb07626f1e54ad7c9d1426a79c485b767d 100644 (file)
@@ -33,6 +33,9 @@ jQuery.extend({
                                        deferred.done( arguments ).fail( arguments );
                                        return this;
                                },
+                               "catch": function( fn ) {
+                                       return promise.then( null, fn );
+                               },
                                // Keep pipe for back-compat
                                pipe: function( /* fnDone, fnFail, fnProgress */ ) {
                                        var fns = arguments;
index 97f9111dd35bab9e536f27458fb93c890b3e52c7..af91b3616988833452f5fdd6b4ddf3b17c32f8ff 100644 (file)
@@ -167,6 +167,42 @@ test( "jQuery.Deferred.then - filtering (fail)", function( assert ) {
        });
 });
 
+test( "jQuery.Deferred.catch", function( assert ) {
+       assert.expect( 4 );
+
+       var value1, value2, value3,
+               defer = jQuery.Deferred(),
+               piped = defer.catch(function( a, b ) {
+                       return a * b;
+               }),
+               done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
+
+       piped.done(function( result ) {
+               value3 = result;
+       });
+
+       defer.fail(function( a, b ) {
+               value1 = a;
+               value2 = b;
+       });
+
+       defer.reject( 2, 3 ).catch(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().catch(function() {
+               assert.ok( false, "then should not be called on resolve" );
+       }).then( done.pop() );
+
+       jQuery.Deferred().reject().catch( jQuery.noop ).done(function( value ) {
+               assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
+               done.pop().call();
+       });
+});
+
 test( "[PIPE ONLY] jQuery.Deferred.pipe - filtering (fail)", function( assert ) {
 
        assert.expect( 4 );