function asyncEvent(){
var dfd = new 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(function working(){
if ( dfd.state() === "pending" ) {
dfd.notify("working... ");
setTimeout(working, 500);
}
}, 1);
// Return the Promise so caller can't change the Deferred
return 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);
}
);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.