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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Validating inputs up front turns a silent infinite loop into a clear, immediate error.
- 2Generic type parameters let one helper slice arrays of any element type safely.
- 3Awaiting inside a sequential loop processes batches one at a time instead of all at once.
Related explainers
typescript
import { CallHandler, ExecutionContext, Injectable,
Wrapping responses in a NestJS interceptor
interceptors
rxjs
response-shaping
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
typescript
import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; @Pipe({ name: 'timeAgo',
A self-refreshing timeAgo pipe in Angular
impure-pipe
change-detection
timers
Advanced
10 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/splitting-work-into-sequential-batches-in-typescript-explained-typescript-daa8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.