# mapWithLimit

> **mapWithLimit**\<`T`, `R`\>(`items`, `concurrency`, `fn`): [`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<`R`[]\>

Defined in: [packages/synapse-core/src/utils/concurrency.ts:63](https://github.com/FilOzone/synapse-sdk/blob/628d3a8812e8612a183a3abe3328169fb741260b/packages/synapse-core/src/utils/concurrency.ts#L63)

Like `Promise.all(items.map(fn))`, but runs at most `concurrency` calls at a time.

## Type Parameters

| Type Parameter |
| ------ |
| `T` |
| `R` |

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `items` | readonly `T`[] | The inputs. |
| `concurrency` | `number` | The maximum number of concurrent calls. |
| `fn` | (`item`, `index`) => [`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<`R`\> | The async work per item. |

## Returns

[`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<`R`[]\>

The results, in input order.

## Example

```ts
import { mapWithLimit } from '@filoz/synapse-core/utils'

const pieces = await mapWithLimit(dataSets, 4, (dataSet) => loadPieces(dataSet))
```