deferred.then()
S2C Home « Objects « deferred.then()
Handler addition.
Description
The deferred.then()
Deferred method, adds handlers to the Deferred object that are called on resolution, rejection or progression.
- The arguments passed to the
deferred.then()
method can benull
if no callback of that type is required. You can also use thedeferred.done()
,deferred.fail()
ordeferred.progress()
methods to set single callback types. - Callbacks are executed in the order they were added and each callback is passed the
args
from the respective method if any were specified. - Any doneCallbacks added after the
Deferred
object enters the resolved state are executed immediately when they are added, using the arguments that were passed to thedeferred.resolve
method. - Any failCallbacks added after the
Deferred
object enters the rejected state are executed immediately when they are added, using the arguments that were passed to thedeferred.reject
method. - Any progressCallbacks added after the
Deferred
object enters the resolved or rejected state are ignored.
From jQuery 1.8 this method has been revamped to replace the deprecated deferred.pipe()
method and should be used from this version onwards.
Syntax
Signature | Description |
---|---|
deferred.then( doneFilter [, failFilter ] | Add handlers to the Deferred object that are called on resolution, rejection or progression. |
Parameters
Parameter | Description | Type |
---|---|---|
doneFilter | A function that is called when the Deferred is resolved. |
Function |
failFilter | An optional function that is called when the Deferred is
rejected. | Function |
progressFilter | An optional function that is called when the Deferred is
notified. | Function |
Return
A Promise
object.
deferred.then( doneFilter [, failFilter ]
[, progressFilter ] )
ExamplesTop
[, progressFilter ] )
Add handlers to the Deferred object that are called on resolution, rejection or progression.
In the example below when we press the left button the first time we create a Deferred
object . We then notify and resolve
the Deferred
object and process any progressCallbacks and doneCallbacks.
When we press the right button the first time we create a Deferred
object . We then notify and reject
the Deferred
object and process any progressCallbacks and failCallbacks.
$(function(){
$('#btn13').one('click', function(){
var ourDeferred = $.Deferred();
ourDeferred.then( eFunc, gFunc, hFunc );
ourDeferred.notify( 'Our deferred was notified. <br>', '#div1');
ourDeferred.resolve( 'Our deferred was resolved. <br><br>', '#div1');
});
$('#btn14').one('click', function(){
var ourDeferred = $.Deferred();
ourDeferred.then( eFunc, gFunc, hFunc );
ourDeferred.notify( 'Our deferred was notified. <br>', '#div1');
ourDeferred.reject( 'Our deferred was rejected. <br><br>', '#div1');
});
function eFunc( value, div ){
$(div).append( 'In eFunc: ' + value);
}
function gFunc( value, div ){
$(div).append( 'In gFunc: ' + value);
}
function hFunc( value, div ){
$(div).append( 'In hFunc: ' + value);
}
});
div1. Some initial text.
In the example below when we press the left button the first time we create a Deferred
object. We then use a then defer that will react to doneCallbacks
from the original Deferred
. We then resolve the original referred which calls back the then defer passing a message. We add to the message passed to the then deferred and output it below.
When we press the centre button the first time we create a Deferred
object. We then use a then defer that will react to failCallbacks
from the original Deferred
. We then reject the original referred which calls back the then defer passing a message. We add to the message passed to the then deferred and output it below.
When we press the right button the first time we create a Deferred
object. We then use a then defer that will react to doneCallbacks and progressCallbacks
from the original Deferred
. We then notify and resolve the original referred which calls back the then defer passing a message each time. We add to the messages passed to the then deferred and output it below.
$(function(){
$('#btn28').one('click', function(){
var ourDeferred = $.Deferred();
thenDefer = ourDeferred.then( function ( value ) {
return value + ' Returning from "thenDefer", so will be resolved. <br><br>';
});
ourDeferred.resolve( 'Resolving "ourDeferred". <br>' );
thenDefer.done(function( value ) {
aFunc( value, '#div2' );
});
});
$('#btn29').one('click', function(){
var ourDeferred = $.Deferred();
thenDefer = ourDeferred.then( null, function ( value ) {
return value + ' Returning from "thenDefer", so will be rejected. <br><br>';
});
ourDeferred.reject( 'Rejecting "ourDeferred". <br>' );
thenDefer.fail(function( value ) {
aFunc( value, '#div2' );
});
});
$('#btn30').one('click', function(){
var ourDeferred = $.Deferred();
thenDefer = ourDeferred.then(
function ( value ) {
return value + ' Returning from "thenDefer", so will be resolved. <br><br>';
},
null,
function ( value ) {
return value + ' Returning from "thenDefer", so will be notified. <br>';
}
);
ourDeferred.notify( 'Notifying "ourDeferred". <br>' );
ourDeferred.resolve( 'Resolving "ourDeferred". <br>' );
thenDefer.progress( function( value ) {
aFunc( value, '#div2' );
});
thenDefer.done( function( value ) {
aFunc( value, '#div2' );
});
});
});
div2. Some initial text.