deferred.always() JQ Home  <<  Objects  <<  deferred.always()

alwaysCallbacks additions.

Description

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

  • When the Deferred is resolved or rejected, callbacks are executed in the order they were added.

Syntax

Signature Description
deferred.always( alwaysCallbacks )Add handlers to be called when the Deferred object is resolved or rejected.

Parameters

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

Return

A Deferred object.

deferred.always( alwaysCallbacks ) Example Objects  <<  Top

Add handlers to be called when the Deferred object is resolved or 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.always( alwaysCallbacks ) 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.always( alwaysCallbacks ) gets called with the message passed from the reject.


$(function(){
  $('#btn1').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.always( aFunc );
    ourDeferred.progress( [aFunc, bFunc] );
    ourDeferred.notify( 'The aFunc function was fired from our deferred. <br>', '#div1');
    ourDeferred.resolve( 'Our deferred was resolved. <br>', '#div1' );
  });
  $('#btn4').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.always( aFunc );
    ourDeferred.progress( aFunc );
    ourDeferred.notify( 'The aFunc function was fired from our deferred. <br>', '#div1' );
    ourDeferred.reject( 'Our deferred was rejected. <br>', '#div1' );
  });

  function aFunc( value, div ){
    $(div).append( value);
  }
  function bFunc( value, div ){
    aFunc('Passing bFunc function value to aFunc. <br>', div);
  }
});

div1. Some initial text.

Press the button below to action the above code:

                

Related Tutorials

jQuery Advanced Tutorials - Lesson 7 - The Deferred Object