Skip to content

Instantly share code, notes, and snippets.

@aklaswad
Created June 15, 2013 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aklaswad/5787844 to your computer and use it in GitHub Desktop.
Save aklaswad/5787844 to your computer and use it in GitHub Desktop.
var async_task = function (msg, delay) {
var dfd = $.Deferred();
setTimeout( function () {
if ( msg !== 'l' ) {
dfd.resolve('resolved:' + msg);
}
else {
dfd.reject('rejected:' + msg);
}
}, delay);
return dfd;
};
// This "then" style works only for jQuery 1.8 or above.
// For 1.7 or below, use deferred.pipe() instead.
// See http://api.jquery.com/deferred.then/#entry-longdesc for more detail.
$.when( async_task('h', 2000) )
.then( function (ret) {
console.log('success', ret);
return async_task('o', 2000);
}, function (ret) {
console.log('\\(^o^)/', ret);
})
.then( function (ret) {
console.log('success', ret);
return async_task('l', 2000);
}, function (ret) {
console.log('-(^o^)-', ret);
})
.then( function (ret) {
console.log('success', ret);
return async_task('a', 2000);
}, function (ret) {
console.log('/(^o^)\\', ret);
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment