Skip to content

Instantly share code, notes, and snippets.

@istarkov
Last active August 8, 2023 20:59
Show Gist options
  • Save istarkov/a42b3bd1f2a9da393554 to your computer and use it in GitHub Desktop.
Save istarkov/a42b3bd1f2a9da393554 to your computer and use it in GitHub Desktop.
Serialize promise calls (run promises sequentially)
// 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]
@sw360cab
Copy link

sw360cab commented Aug 4, 2022

works as a charm 👏 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment