Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created May 28, 2018 13:57
Show Gist options
  • Save fakenickels/2723f1b450c7fd2086f3f15a980204e9 to your computer and use it in GitHub Desktop.
Save fakenickels/2723f1b450c7fd2086f3f15a980204e9 to your computer and use it in GitHub Desktop.
Waterfall helper
const mail = (foo, t) => new Promise((resolve) => setTimeout(() => { console.log(foo); resolve() }, t))
async function doThing() {
const items = ['foo', 'bar', 'buz', 'fuz'];
await waterfall(items.map((item) => async () => { await mail('mailing' + item, 4000); }))
await mail("end", 1000)
}
const waterfall = (promises) => promises.reduce((wfall, currentPromise) => wfall.then(currentPromise), Promise.resolve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment