Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created February 19, 2021 11:19
Show Gist options
  • Save andrIvash/2930d6c68bcc0b4b48522c94c255414c to your computer and use it in GitHub Desktop.
Save andrIvash/2930d6c68bcc0b4b48522c94c255414c to your computer and use it in GitHub Desktop.
Async callback calls for array items
/**
* 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