Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Created February 24, 2020 19:25
Show Gist options
  • Select an option

  • Save bmorrisondev/39567b9d1433687ec522b7fd7058a240 to your computer and use it in GitHub Desktop.

Select an option

Save bmorrisondev/39567b9d1433687ec522b7fd7058a240 to your computer and use it in GitHub Desktop.
An async version of an array forEach loop in JavaScript
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
// Usage:
// await asyncForEach(myArray, async (element) => {
// await someLongRunningOperation(element);
// await someOtherLongRunningOp(element);
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment