jQuery & jQuery UI Documentation

jQuery & jQuery UI

deferred.then()

deferred.then( doneCallbacks, failCallbacks ) Returns: Deferred

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 the deferred object, other methods of the deferred object can be chained to this one, including additional .then() methods. For more information, see the documentation for Deferred object.

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!"); }
);