jQuery deferred.then() method
Example
Add a .then method
$
(
function
(
)
{
$.
get
(
"
test.php
"
)
.
then
(
function
(
)
{
alert
(
"
$.get succeeded
"
)
;
}
,
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 still in progress.
**Note:** 1. Arguments can be null. Alternatively, use .done(), .fail(), or .progress() to set a callback type for only one 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. Callbacks are executed in the order in which they were added. Since deferred.then returns a Promise object, other Promise objects can be chained, 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 called when the Deferred object is resolved. |
| failFilter | Function type: Optional, a function called when the Deferred object is rejected. |
| progressFilter | Function type: Optional, a function called when the Deferred object generates a progress notification. |
| 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 a progress notification. |
More examples
Filter resolved values
Filter resolved values.
Filter rejected values
Filter rejected values.
Chain tasks
Chain tasks.
jQuery Miscellaneous Methods