-
-
Save gmilby/5614316 to your computer and use it in GitHub Desktop.
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