Skip to content

Instantly share code, notes, and snippets.

@giordanocardillo
Created May 30, 2018 12:59
Show Gist options
  • Select an option

  • Save giordanocardillo/aa092f99268db560aef8215e60d69761 to your computer and use it in GitHub Desktop.

Select an option

Save giordanocardillo/aa092f99268db560aef8215e60d69761 to your computer and use it in GitHub Desktop.
JS ForEach with async wait
const asyncFn = (param) => {
return new Promise((resolve, reject) => {
setTimeout(()=>{
console.info(param)
resolve(param)
}, 1e3)
})
}
console.info('before');
[1, 2, 3].reduce((promiseChain, arrayItem) =>
promiseChain.then(() => asyncFn(arrayItem)), Promise.resolve()).then(()=>{
console.info('after')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment