JSFiddle - React, Tailwind, and code Playground
by Juan Muñoz
JavaScript
var valor, valor2;
function asyncEvent() {
var dfd = jQuery.Deferred();
// Resolve after a random interval
setTimeout(function() {
dfd.resolve( "hurray" );
valor = Math.floor( 400 + Math.random() * 2000 );
console.log("hurray"+valor);
}, valor );
// Reject after a random interval
setTimeout(function() {
dfd.reject( "sorry" );
valor2 = Math.floor( 400 + Math.random() * 2000 );
console.log("sorry"+valor2);
}, valor2);
// 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 ) {
console.log("1--"+status)
alert( status + ", things are going well" );
},
function( status ) {
console.log("2--"+status)
alert( status + ", you fail this time" );
},
function( status ) {
console.log("3--"+status)
$( "body" ).append( status );
}
);