-
-
Save zburgermeiszter/0c8aa55ef6cecb17ec5e33c592f59290 to your computer and use it in GitHub Desktop.
Serialize promise calls (run 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
// promise | |
const sleep = (timeout, v) => new Promise(r => setTimeout(() => {console.log(v); r(v)}, timeout)); | |
// series to call | |
const series = [() => sleep(1000, 1), () => sleep(1000, 2), () => sleep(1000, 3)]; | |
// serialize | |
const r = series | |
.reduce( | |
(m, p) => m.then(v => Promise.all([...v, p()])), | |
Promise.resolve([]) | |
); | |
// get result | |
r.then((r) => console.log('done', r)) | |
// out [1, 2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment