Created
October 16, 2012 21:06
-
-
Save johnhunter/3902061 to your computer and use it in GitHub Desktop.
using jquery promise
This file contains hidden or 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
// using a promise | |
$.when(doStuff('eat'), doStuff('sleep')).then(function(a, b){ | |
console.log(a + ' and ' + b +' are done'); | |
}); | |
// define the function that returns a promise | |
function doStuff (subject) { | |
var defer = new $.Deferred(); | |
console.log('starting '+ subject +'...'); | |
// do some async action | |
setTimeout(function () { | |
if (subject) defer.resolve(subject); | |
else defer.reject(); | |
}, 1000); | |
// return the promise immediately | |
return defer.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
handle 2 async promises