deferred.then()
deferred.then( doneCallbacks, failCallbacks ) Returns: Promise
Description: Add handlers to be called when the Deferred object is resolved or rejected.
-
version added: 1.5deferred.then( doneCallbacks, failCallbacks )
doneCallbacks A function, or array of functions, called when the Deferred is resolved.
failCallbacks A function, or array of functions, called when the Deferred is rejected.
-
version added: 1.7deferred.then( doneCallbacks, failCallbacks [, progressCallbacks] )
doneCallbacks A function, or array of functions, called when the Deferred is resolved.
failCallbacks A function, or array of functions, called when the Deferred is rejected.
progressCallbacks A function, or array of functions, called when the Deferred notifies progress.
All three arguments (including progressCallbacks, as of jQuery 1.7) can be either a single function or an array of functions. The arguments can also be null
if no callback of that type is desired. Alternatively, use .done()
, .fail()
or .progress()
to set only one type of callback.
When the Deferred is resolved, the doneCallbacks are called. If the Deferred is instead rejected, the failCallbacks are called. As of jQuery 1.7, the deferred.notify()
or deferred.notifyWith()
methods can be called to invoke the progressCallbacks as many times as desired before the Deferred is resolved or rejected.
Callbacks are executed in the order they were added. Since deferred.then
returns a Promise, other methods of the Promise object can be chained to this one, including additional .then()
methods.
Example:
Since the jQuery.get method returns a jqXHR object, which is derived from a Deferred object, we can attach handlers using the .then method.
$.get("test.php").then(
function(){ alert("$.get succeeded"); },
function(){ alert("$.get failed!"); }
);
Support and Contributions
Need help with deferred.then() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.
Think you've discovered a jQuery bug related to deferred.then()? Report it to the jQuery core team.
Found a problem with this documentation? Report it on the GitHub issue tracker