Async
aForEach

Array forEach with Async callback function.

Syntax

aForEach(
  arr: T[], 
  cb: (value: T, index: number) => Promise<void>
): Promise<undefined>

Usage

import { aForEach } from "@opentf/std";
 
aForEach([], async (value, index) => {});

Examples

const flatten = async (val) => {
  return new Promise((resolve) => {
    Array.isArray(val) ? resolve(val.flat()) : resolve(val);
  });
};
 
const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]];
const tmpArr = [];
await aForEach(nested, async (n) => {
  const val = await flatten(n);
  Array.isArray(val) ? tmpArr.push(...val) : tmpArr.push(val);
});
console.log(tmpArr) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

Try