If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return
only
the Promise object via
deferred.promise()
so other code can register callbacks or inspect the current state.
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.
var dfd = jQuery.Deferred();
}, Math.floor( 400 + Math.random() * 2000 ) );
}, Math.floor( 400 + Math.random() * 2000 ) );
setTimeout(function working() {
if ( dfd.state() === "pending" ) {
dfd.notify( "working... " );
setTimeout( working, 500 );
$.when( asyncEvent() ).then(
alert( status + ", things are going well" );
alert( status + ", you fail this time" );
$( "body" ).append( status );
hello: function( name ) {
alert( "Hello " + name );
obj.done(function( name ) {