Created
June 15, 2013 11:37
-
-
Save aklaswad/5787844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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