Created
June 16, 2017 20:18
-
-
Save daronwolff/d13995847bfc49ae2ad0fc8f360fe6e5 to your computer and use it in GitHub Desktop.
Executing promises sequentially
This file contains hidden or 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
const times = [ 1, 3, 4, 2 ]; | |
const sleep = ms => | |
new Promise(res => { | |
const t = ms * 1000; | |
setTimeout(res, t) | |
}) | |
const myPromise = num => | |
sleep(num).then(() => { | |
console.log('done: ' + num) | |
}) | |
times.reduce( | |
(p, x) => | |
p.then(_ => myPromise(x)), | |
Promise.resolve() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment