typescript 25 lines · 5 steps

Splitting work into sequential batches in TypeScript

A generic chunk helper feeds an async runner that processes batches one at a time.

Explained by highlit
1export function chunk<T>(items: readonly T[], size: number): T[][] {
2 if (size <= 0 || !Number.isInteger(size)) {
3 throw new RangeError(`chunk size must be a positive integer, got ${size}`);
4 }
5 
6 const batches: T[][] = [];
7 for (let i = 0; i < items.length; i += size) {
8 batches.push(items.slice(i, i + size));
9 }
10 return batches;
11}
12 
13export async function processInBatches<T, R>(
14 items: readonly T[],
15 size: number,
16 handler: (batch: T[], index: number) => Promise<R>,
17): Promise<R[]> {
18 const batches = chunk(items, size);
19 const results: R[] = [];
20 
21 for (let index = 0; index < batches.length; index++) {
22 results.push(await handler(batches[index], index));
23 }
24 return results;
25}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Validating inputs up front turns a silent infinite loop into a clear, immediate error.
  2. 2Generic type parameters let one helper slice arrays of any element type safely.
  3. 3Awaiting inside a sequential loop processes batches one at a time instead of all at once.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Splitting work into sequential batches in TypeScript — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code