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;}Report a problem with this exampleOpens a pre-filled issue on GitHub.