Skip to content

Instantly share code, notes, and snippets.

@smagch
Created September 13, 2016 12:49
Show Gist options
  • Save smagch/d604a96a7928f3dfd940af24e1b312cb to your computer and use it in GitHub Desktop.
Save smagch/d604a96a7928f3dfd940af24e1b312cb to your computer and use it in GitHub Desktop.
Sequential Promise execution
// StackOverFlow: http://stackoverflow.com/a/30823708/1248952
function getTimer(name, timeout) {
return () => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(name);
resolve();
}, timeout);
});
}
let p1 = getTimer('p1', 200);
let p2 = getTimer('p2', 10);
let p3 = getTimer('p3', 50);
[p1, p2, p3].reduce((p, fn) => p.then(fn), Promise.resolve()).then(() => {
console.log('end');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment