Skip to content

Instantly share code, notes, and snippets.

@syzer
Created October 20, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syzer/8df4e0753bcf7305b49ae49e5f9b7f20 to your computer and use it in GitHub Desktop.
Save syzer/8df4e0753bcf7305b49ae49e5f9b7f20 to your computer and use it in GitHub Desktop.
Use reduce for serial async resolution AKA map.series
const asyncFunction = (nextID) =>
new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`Called with ${new Date()}`)
resolve()
}, 1000)
})
;[1, 2, 3].reduce((accumulatorPromise, nextID) => {
console.log(`Looped with ${new Date()}`)
return accumulatorPromise
.then(() =>
asyncFunction(nextID))
}, Promise.resolve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment