Cancellation and concurrency control · senior
Concurrency limit: the promise pool
A promise pool runs many async tasks with at most N in flight: start N workers, and each time a task finishes start the next one, storing each result at its input index so the output order matches the input.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Tasks must be functions that start work (thunks), not promises: a promise has already started.
- Two common shapes: N worker loops pulling from a shared index (while (next < tasks.length)), or launch-on-complete with counters.
- Store results by index. Resolve when the completed count equals the total, not when the index reaches the end.
- Error modes: fail fast (reject on the first error and start nothing new), collect all (like allSettled), or call an error callback and continue (Uber's follow-up).
- Why not batches of N with Promise.all? One slow task blocks its whole batch. A pool keeps every slot busy.
- Real uses: uploading files, fetching 100 URLs, image processing, rate-sensitive APIs. Browsers allow about 6 HTTP/1.1 connections per host; HTTP/2 multiplexes on one connection.
- Extensions: priority, pause and resume, per-task timeout, retries, progress events, adding tasks while running (a queue class).
- This is the most repeated advanced utility in 2025–2026 reports: Flipkart (2026), Uber (several), Zeta (2025).
The red pen
Common traps: mistake → fix
Mistake: Pass promises into the pool
Fix: they are already running; pass functions.
Mistake: Split into batches of N
Fix: slow tasks waste the other slots.
Mistake: Resolve when the last task starts
Fix: resolve when every task has finished.
Easy to hard
Questions interviewers ask, easy to hard
- Fetch 100 URLs with at most 3 in flight and return results in order.
- What should happen when a task fails?
- Why is a pool better than batches?
- Add priorities, pause/resume and adding tasks later.
- Write it as a TypeScript class with typed results and a pluggable executor (Uber L4, 2025).
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.