Skip to content

Instantly share code, notes, and snippets.

@gmilby
Forked from johnhunter/promise-example.js
Created May 20, 2013 18:43
Show Gist options
  • Save gmilby/5614316 to your computer and use it in GitHub Desktop.
Save gmilby/5614316 to your computer and use it in GitHub Desktop.
// 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