deferred.fail()S2C Home « Objects « deferred.fail()

failCallbacks additions.

Description

The deferred.fail() Deferred method, adds handlers to be called when the Deferred object is rejected.


Syntax

Signature Description
deferred.fail( failCallbacks )Add handlers to be called when the Deferred object is rejected.

Parameters

Parameter Description Type
failCallbacksA function(s), or array(s) of functions, that are called when the Deferred is rejected.Function

Return

A Deferred object.

deferred.fail( failCallbacks ) ExampleTop

Add handlers to be called when the Deferred object is rejected.

In the example below when we press the left button the first time we create a Deferred object and use some deferred object methods on it. We then resolve the Deferred object and hence deferred.done( doneCallbacks ) gets called with the message passed from the resolve.

When we press the right button the first time we create a Deferred object and use some deferred object methods on it. We then reject the Deferred object and hence deferred.fail( failCallbacks ) gets called with the message passed from the reject.


$(function(){
  $('#btn5').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( cFunc );
    ourDeferred.fail( dFunc );
    ourDeferred.progress( cFunc, dFunc );
    ourDeferred.notify( 'function was fired from our deferred. <br>', '#div1');
    ourDeferred.resolve( 'Our deferred was resolved. <br> <br>', '#div1' );
  });
  $('#btn6').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( cFunc );
    ourDeferred.fail( dFunc );
    ourDeferred.progress( cFunc, dFunc );
    ourDeferred.notify( 'function was fired from our deferred. <br>', '#div1');
    ourDeferred.reject( 'Our deferred was rejected. <br> <br>', '#div1' );
  });

  function cFunc( value, div ){
    $(div).append( 'In cFunc: ' + value);
  }
  function dFunc( value, div ){
    $(div).append( 'In dFunc: ' + value);
  }
});

div1. Some initial text.

Press the button below to action the above code: