import TryIt from '../../../components/TryIt.jsx';
export const tryItCode = "const items = [1, 2, 3];\nconsole.log(await flatMapAsync(items, async (n) => [n, n * 2])); //=> [1, 2, 2, 4, 3, 6]";
import { Callout } from "@opentf/afw-docs";
export const metadata = {
title: "flatMapAsync — Concurrency",
description: "Asynchronous version of \\Array.prototype.flatMap\\."
};
# flatMapAsync
⚡ Asynchronous version of \`Array.prototype.flatMap\`.
By default, it runs all iterations in **parallel**. You can limit the
concurrency through an options object.
## Syntax
```ts
import { flatMapAsync } from '@opentf/std';
flatMapAsync(
arr: T[],
cb: (value: T, index: number) => Promise,
options?: { concurrency?: number }
): Promise;
```
## 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
```ts
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]
```
## Related
- [mapAsync](/docs/Concurrency/mapAsync)
- [filterAsync](/docs/Concurrency/filterAsync)
## Try it