Async Pool Concurrent Map Typed
Run async tasks in a typed concurrency pool collecting results in order with proper type inference.
Code
export async function asyncPool<T,R>(items:readonly T[],worker:(item:T,index:number)=>Promise<R>,concurrency=4):Promise<R[]>{const results:R[]=new Array(items.length);let next=0;async function run(){while(next<items.length){const i=next++;results[i]=await worker(items[i],i);}}await Promise.all(Array.from({length:Math.min(concurrency,items.length)},run));return results;}Inside the snippet
The working surface is the functions asyncPool and run — everything else is wiring.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Using it
asyncPool is what another module would consume; nothing else here is load-bearing for callers.
FAQ
- Are there third-party dependencies?
- No. The dependency list is empty; the code stays inside the standard library. The file defines `asyncPool`, `run` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.