Promise Pool With a Concurrency Limit
Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.
JavaScript · ES6 — Pratik Rai ·
Collapse many individual lookups made in the same tick into one request, then hand each caller its own result.
Write createBatcher(fetchMany). It returns a function that takes a single id and returns a promise for that id's value.
Every id requested before the next microtask is collected and passed to fetchMany as one array. fetchMany(ids) resolves to an array of values in the same order, and each caller receives only its own.
Input:
JSfile.javascript1const load = createBatcher(async (ids) => { 2 console.log('one request for', ids); 3 return ids.map((id) => 'user-' + id); 4}); 5 6await Promise.all([load(1), load(2), load(3)]);
Output:
one request for [1, 2, 3]
['user-1', 'user-2', 'user-3']
Three calls, one request.
A list of fifty rows, each asking for its own author, produces fifty requests. Batching collapses them: collect every id requested in the same tick, ask once, and hand each caller its slice.
A queue plus a scheduled flush. The first push schedules; later pushes in the same tick just join.
JSfile.javascript1function createBatcher(fetchMany) { 2 let queue = []; 3 return (id) => new Promise((resolve, reject) => { 4 queue.push({ id, resolve, reject }); 5 if (queue.length > 1) return; 6 Promise.resolve().then(() => { 7 const batch = queue; 8 queue = []; 9 fetchMany(batch.map((item) => item.id)).then( 10 (values) => batch.forEach((item, i) => item.resolve(values[i])), 11 (error) => batch.forEach((item) => item.reject(error)) 12 ); 13 }); 14 }); 15}
Promise.resolve().then(...) — a microtask — runs after the current synchronous block but before any timer, so everything requested "in the same tick" is caught without adding a millisecond of latency.
Swapping queue for a fresh array before awaiting matters: calls made while the request is in flight must start a new batch rather than joining one that has already left. This is what DataLoader does, and how a GraphQL server avoids the N+1 problem.
Goal: One request per tick, with every caller resolved from the batched response.
Promise.resolve().then(...) runs after the current synchronous block but before any timer, so it catches everything requested in the same tick without adding measurable delay. A setTimeout(0) would work but costs at least a frame.Continue learning with these related challenges
Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.
JavaScript · ES6 — Pratik Rai ·
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
JavaScript · ES6 — Pratik Rai ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6 — Pratik Rai ·
Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.
JavaScript · ES6
Pratik Rai ·
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
JavaScript · ES6
Pratik Rai ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6
Pratik Rai ·