JS Machine Coding Questions — Implementations You Must Know
Parallel limit (async pool)
- Run many async tasks but only N at a time — e.g. crawl URLs without opening 100 connections.
- Key insight: Maintain a pool of N worker loops; each worker pulls the next index from a shared counter until the list is exhausted.
- Gotcha: Pass functions (() => doWork(i)) so work starts only when a slot is free — not an array of already-started promises.
- Gotcha: Wrap await tasks[i]() in try/catch or use Promise.allSettled on the pool if partial failure is OK.
1async function parallelLimit(tasks, limit) {
2 const results = new Array(tasks.length);
3 let nextIndex = 0;
4
5 async function worker() {
6 while (true) {
7 const i = nextIndex++;
8 if (i >= tasks.length) break;
9 results[i] = await tasks[i]();
10 }
11 }
12
13 const pool = Array.from(
14 { length: Math.min(limit, tasks.length) },
15 () => worker()
16 );
17 await Promise.all(pool);
18 return results;
19}
20
21// Usage: tasks[i] is a zero-arg function returning a Promise
22await parallelLimit(
23 urls.map((url) => () => fetch(url).then((r) => r.json())),
24 3
25);