Add handlers to be called when the Deferred object is either resolved or rejected.
see
``
since
1.6
example
````Since the jQuery.get() method returns a jqXHR object, which is derived from a Deferred object, we can attach a callback for both success and error using the deferred.always() method.
$.get( "test.php" ).always(function() {
alert( "$.get completed with success or error callback arguments" );
});
Add handlers to be called when the Deferred object is resolved.
see
``
since
1.5
example
````Since the jQuery.get method returns a jqXHR object, which is derived from a Deferred object, we can attach a success callback using the .done() method.
````Resolve a Deferred object when the user clicks a button, triggering a number of callback functions:
<!doctype html><htmllang="en"><head><metacharset="utf-8"><title>deferred.done demo</title><scriptsrc="https://code.jquery.com/jquery-3.3.1.js"></script></head><body>
<button>Go</button><p>Ready...</p>
<script>// 3 functions to call when the Deferred object is resolvedfunctionfn1() {
$( "p" ).append( " 1 " );
}
functionfn2() {
$( "p" ).append( " 2 " );
}
functionfn3( n ) {
$( "p" ).append( n + " 3 " + n );
}
// Create a deferred objectvar dfd = $.Deferred();
// Add handlers to be called when dfd is resolved
dfd
// .done() can take any number of functions or arrays of functions
.done( [ fn1, fn2 ], fn3, [ fn2, fn1 ] )
// We can chain done methods, too
.done(function( n ) {
$( "p" ).append( n + " we're done." );
});
// Resolve the Deferred object when the button is clicked
$( "button" ).on( "click", function() {
dfd.resolve( "and" );
});
</script>
</body></html>
Add handlers to be called when the Deferred object is rejected.
see
``
since
1.5
example
````Since the jQuery.get method returns a jqXHR object, which is derived from a Deferred, you can attach a success and failure callback using the deferred.done() and deferred.fail() methods.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter resolve value:
var defer = $.Deferred(),
filtered = defer.pipe(function( value ) {
return value * 2;
});
defer.resolve( 5 );
filtered.done(function( value ) {
alert( "Value is ( 2*5 = ) 10: " + value );
});
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.pipe( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.pipe( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: null
An optional function that is called when the Deferred is resolved.
failFilter: function
An optional function that is called when the Deferred is rejected.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter resolve value:
var defer = $.Deferred(),
filtered = defer.pipe(function( value ) {
return value * 2;
});
defer.resolve( 5 );
filtered.done(function( value ) {
alert( "Value is ( 2*5 = ) 10: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: null
An optional function that is called when the Deferred is resolved.
failFilter: null
An optional function that is called when the Deferred is rejected.
Optional progressFilter: function
An optional function that is called when progress notifications are sent to the Deferred.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter resolve value:
var defer = $.Deferred(),
filtered = defer.pipe(function( value ) {
return value * 2;
});
defer.resolve( 5 );
filtered.done(function( value ) {
alert( "Value is ( 2*5 = ) 10: " + value );
});
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.pipe( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.pipe( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
Parameters
doneFilter: null
An optional function that is called when the Deferred is resolved.
failFilter: function
An optional function that is called when the Deferred is rejected.
Cause: The .pipe() method on a jQuery.Deferred object was deprecated as of jQuery 1.8, when the .then() method was changed to perform the same function.
Solution: In most cases it is sufficient to change all occurrences of .pipe() to .then(). Ensure that you aren't relying on context/state propagation (e.g., using this) or synchronous callback invocation, which were dropped from .then() for Promises/A+ interoperability as of jQuery 3.0.
example
````Filter resolve value:
var defer = $.Deferred(),
filtered = defer.pipe(function( value ) {
return value * 2;
});
defer.resolve( 5 );
filtered.done(function( value ) {
alert( "Value is ( 2*5 = ) 10: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.pipe(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
````Use the target argument to promote an existing object to a Promise:
// Existing objectvar obj = {
hello: function( name ) {
alert( "Hello " + name );
}
},
// Create a Deferred
defer = $.Deferred();
// Set object as a promise
defer.promise( obj );
// Resolve the deferred
defer.resolve( "John" );
// Use the object as a Promise
obj.done(function( name ) {
obj.hello( name ); // Will alert "Hello John"
}).hello( "Karl" ); // Will alert "Hello Karl"
Type parameters
TTarget: object
Parameters
target: TTarget
Object onto which the promise methods have to be attached
````Create a Deferred and set two timer-based functions to either resolve or reject the Deferred after a random interval. Whichever one fires first "wins" and will call one of the callbacks. The second timeout has no effect since the Deferred is already complete (in a resolved or rejected state) from the first timeout action. Also set a timer-based progress notification function, and call a progress handler that adds "working..." to the document body.
functionasyncEvent() {
var dfd = jQuery.Deferred();
// Resolve after a random interval
setTimeout(function() {
dfd.resolve( "hurray" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Reject after a random interval
setTimeout(function() {
dfd.reject( "sorry" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Show a "working..." message every half-second
setTimeout(functionworking() {
if ( dfd.state() === "pending" ) {
dfd.notify( "working... " );
setTimeout( working, 500 );
}
}, 1 );
// Return the Promise so caller can't change the Deferredreturn dfd.promise();
}
// Attach a done, fail, and progress handler for the asyncEvent
$.when( asyncEvent() ).then(
function( status ) {
alert( status + ", things are going well" );
},
function( status ) {
alert( status + ", you fail this time" );
},
function( status ) {
$( "body" ).append( status );
}
);
var defer = $.Deferred(),
filtered = defer.then( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: function
A function that is called when the Deferred is resolved.
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
see
``
since
1.8
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.then( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: null
A function that is called when the Deferred is resolved.
failFilter: function
An optional function that is called when the Deferred is rejected.
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: function
A function that is called when the Deferred is resolved.
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
see
``
since
1.8
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARP
AJP
ANP
BRP
BJP
BNP
CRP
CJP
CNP
RRP
RJP
RNP
Parameters
doneFilter: null
A function that is called when the Deferred is resolved.
failFilter: null
An optional function that is called when the Deferred is rejected.
Optional progressFilter: function
An optional function that is called when progress notifications are sent to the Deferred.
var defer = $.Deferred(),
filtered = defer.then( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
see
``
since
1.8
example
````Filter reject value:
var defer = $.Deferred(),
filtered = defer.then( null, function( value ) {
return value * 3;
});
defer.reject( 6 );
filtered.fail(function( value ) {
alert( "Value is ( 3*6 = ) 18: " + value );
});
example
````Chain tasks:
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARF
AJF
ANF
BRF
BJF
BNF
CRF
CJF
CNF
RRF
RJF
RNF
Parameters
doneFilter: null
An optional function that is called when the Deferred is resolved.
failFilter: function
An optional function that is called when the Deferred is rejected.
var request = $.ajax( url, { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( url2, { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
Type parameters
ARD
AJD
AND
BRD
BJD
BND
CRD
CJD
CND
RRD
RJD
RND
Parameters
doneFilter: function
An optional function that is called when the Deferred is resolved.
Add handlers to be called when the Deferred object is either resolved or rejected.
``
1.6
````Since the jQuery.get() method returns a jqXHR object, which is derived from a Deferred object, we can attach a callback for both success and error using the deferred.always() method.
$.get( "test.php" ).always(function() { alert( "$.get completed with success or error callback arguments" ); });