Skip to content

Instantly share code, notes, and snippets.

@pfeilbr
Created January 6, 2016 16:49
Show Gist options
  • Save pfeilbr/9365eea196ab4ad5a3df to your computer and use it in GitHub Desktop.
Save pfeilbr/9365eea196ab4ad5a3df to your computer and use it in GitHub Desktop.
// Promise series example
function task(name) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(name, new Date());
if (name === 'fail') {
reject(name);
} else {
resolve(name);
}
}, 2000);
});
}
task('t1')
.then(function(resp) {
return task('t2');
})
.then(function(resp) {
return task('fail'); // force fail
})
.then(function(resp) {
return task('t3');
})
.then(function(resp) {
console.log(resp, 'done');
})
.catch(function(err) {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment