flatMapAsync
⚡ Asynchronous version of `Array.prototype.flatMap`.
Info
By default, it runs all iterations in parallel. You can limit the concurrency through an options object.
Syntax
TypeScript
import { flatMapAsync } from '@opentf/std'; flatMapAsync<T, R>( arr: T[], cb: (value: T, index: number) => Promise<R | R[]>, options?: { concurrency?: number } ): Promise<R[]>;
Parameters
arr: The array to iterate over.cb: An async callback function for each element.options.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] const limited = await flatMapAsync(items, async (n) => [n, n * 2], { concurrency: 2, }); //=> [1, 2, 2, 4, 3, 6]