File size: 760 Bytes
bdbd3b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /**
* Maps every item through an async function, a bounded number of them in
* flight at once, and resolves to the results in the items' order.
*
* @param items the inputs
* @param limit how many may run at once, at least one
* @param fn the work for one item
* @returns the results, in order
*/
export async function mapLimited<T, R>(
items: readonly T[],
limit: number,
fn: (item: T) => Promise<R>,
): Promise<R[]> {
const results: R[] = []
let next = 0
async function worker(): Promise<void> {
for (let at = next++; at < items.length; at = next++) {
results[at] = await fn(items[at] as T)
}
}
await Promise.all(
Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, worker),
)
return results
}
|