spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

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