Last active
August 8, 2023 20:59
-
-
Save istarkov/a42b3bd1f2a9da393554 to your computer and use it in GitHub Desktop.
Serialize promise calls (run promises sequentially)
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
// promise | |
const sleep = (timeout, v) => new Promise(r => setTimeout(() => 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
works as a charm 👏 !