Created
February 24, 2020 19:25
-
-
Save bmorrisondev/39567b9d1433687ec522b7fd7058a240 to your computer and use it in GitHub Desktop.
An async version of an array forEach loop in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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