Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created October 16, 2012 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save johnhunter/3902061 to your computer and use it in GitHub Desktop.
Save johnhunter/3902061 to your computer and use it in GitHub Desktop.
using jquery promise
// 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();
}
@johnhunter
Copy link
Author

handle 2 async promises

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment