⚡ Asynchronous version of `Array.prototype.flatMap`.
Info
By default, it runs all iterations in parallel. You can limit the concurrency by providing a third argument.
Syntax
TypeScript
import { flatMapAsync } from '@opentf/std'; flatMapAsync<T, R>( arr: T[], cb: (value: T, index: number) => Promise<R | R[]>, concurrency?: number ): Promise<R[]>;
Parameters
arr: The array to iterate over.cb: An async callback function for each element.concurrency(Optional): The maximum number of concurrent executions. Default:Infinity.
Returns
A Promise that resolves to the flattened mapped array.
Sparse array holes are skipped, matching native Array.prototype.flatMap() behavior.
Examples
TypeScript
const items = [1, 2, 3]; const result = await flatMapAsync(items, async (n) => { return [n, n * 2]; }); //=> [1, 2, 2, 4, 3, 6]