Promise Pool
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.
Goal: One request per tick, with every caller resolved from the batched response.
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 ·