Created
September 13, 2016 12:49
-
-
Save smagch/d604a96a7928f3dfd940af24e1b312cb to your computer and use it in GitHub Desktop.
Sequential Promise execution
This file contains 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
// 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