jQuery deferred.then()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Example

Add .then method

$(function () { $.get("test.php").then( function(){ alert("$.get success"); }, function(){ alert("$.get failed!"); } ); })

Try it »

Definition and Usage

The deferred.then() function calls the added handlers when the Deferred object is resolved, rejected, or is still in progress.


Note:1. The parameters can be null. Or use .done(), .fail(), or .progress() to set only one type of callback for an unfiltered state or value.
2. Since jQuery 1.8, the method returns a new promise, which can filter the state and value of the deferred object through a function, replacing the now deprecated deferred.pipe() method.
3. The callbacks are executed in the order they were added. Since deferred.then returns a Promise object, it can be chained with other Promise objects, including additional .then() methods.


Syntax

Method 1

deferred.then( doneFilter [, failFilter ] [, progressFilter ] )

Method 2

deferred.then( doneCallbacks, failCallbacks [, progressCallbacks ] )

Parameters Description
doneFilter Function type Optional. A function that is called when the Deferred object is resolved.
failFilter Function type Optional. A function that is called when the Deferred object is rejected.
progressFilter Function type Optional. A function that is called when the Deferred object generates progress notifications.
Parameters Description
doneCallbacks Function type. A function or an array of functions called when the Deferred object is resolved.
failCallbacks Function type. A function or an array of functions called when the Deferred object is rejected.
progressCallbacks Function type. A function or an array of functions called when the Deferred object generates progress notifications.


实例

More Examples

Filter resolved values
Filter resolved values.

Filter rejected values
Filter rejected values.

Chain tasks
Chain tasks.


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions