Skip to content

Instantly share code, notes, and snippets.

@phellipeandrade
Forked from atinux/async-foreach.js
Created February 25, 2018 04:35
Show Gist options
  • Save phellipeandrade/6a7034c4f5c9a8cf0fe66cfca6ff6762 to your computer and use it in GitHub Desktop.
Save phellipeandrade/6a7034c4f5c9a8cf0fe66cfca6ff6762 to your computer and use it in GitHub Desktop.
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)
console.log(num)
})
console.log('Done')
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment