Created
February 19, 2021 11:19
-
-
Save andrIvash/2930d6c68bcc0b4b48522c94c255414c to your computer and use it in GitHub Desktop.
Async callback calls for array items
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 callback calls for array items | |
* https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404 | |
* | |
* | |
* await asyncForEach(items, async (item) => { | |
* await someAsyncCallback(items) | |
* .catch((e) => { | |
* console.error(e); | |
* }); | |
* console.log("done one"); | |
* }); | |
* console.log("done all"); | |
* | |
* @param array | |
* @param callback | |
*/ | |
export const asyncForEach = async (array: any[], callback: (item: any, index: number, array: any[]) => | |
Promise<void>) => { | |
for (let index = 0; index < array.length; index++) { | |
await callback(array[index], index, array); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment