typescript / intermediate
Snippet
Controlled Async Batch Processing using Promise.allSettled
Executing hundreds of async calls simultaneously can overload resources. Processing data in defined chunks with Promise.allSettled limits concurrent execution while accurately preserving both fulfilled and rejected task outcomes.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
async function processInBatches<T, R>(items: T[],batchSize: number,task: (item: T) => Promise<R>): Promise<PromiseSettledResult<R>[]> {const results: PromiseSettledResult<R>[] = [];for (let i = 0; i < items.length; i += batchSize) {const chunk = items.slice(i, i + batchSize);const chunkResults = await Promise.allSettled(chunk.map(task));results.push(...chunkResults);}return results;}
Breakdown
1
const chunk = items.slice(i, i + batchSize);
Slices the original dataset into a small batch chunk for controlled execution.
2
const chunkResults = await Promise.allSettled(chunk.map(task));
Runs all promises in the chunk concurrently and waits for completion without throwing on individual rejections.