js deferred test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
function asyncEvent() {
var dfd = new jQuery.Deferred();
// Resolve after a random interval
setTimeout(function() {
dfd.resolve( "hurray" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// 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) {
$( "#content" ).append( status );
}
);