Time Out an Async Function
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
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.
Given an array of functions that each return a promise, and a number n, run them all but never more than n at the same time.
As soon as one finishes, the next should start — do not wait for a whole batch to complete. Resolve once every function has finished.
Input:
JSfile.javascript1const wait = (ms) => () => new Promise((r) => setTimeout(r, ms)); 2 3const t0 = Date.now(); 4await promisePool([wait(200), wait(500), wait(100), wait(250)], 2); 5Date.now() - t0;
Output:
~550
The 200ms and 500ms tasks start together. The 200ms one frees its slot first, so the 100ms task runs from 200ms to 300ms, and the 250ms task then runs until 550ms. Fixed batches of two would have taken 500 + 250 = 750ms.
Promise.all starts everything at once. When "everything" is fifty network requests, that is a stampede — the browser queues them anyway, the server sees a spike, and the slowest response still gates the result. A pool caps how many run concurrently.
The naive fix is batching: slice the list into groups of n and await each group. It is wrong in a way that is easy to miss — a group cannot start until the slowest member of the previous one finishes, so a fast task sits idle behind a slow one.
The fix is to think in workers rather than batches. Start n of them; each pulls the next unclaimed task, awaits it, and loops. A worker that finishes early immediately takes more work.
JSfile.javascript1async function promisePool(functions, n) { 2 let next = 0; 3 const worker = async () => { 4 while (next < functions.length) { 5 const index = next; 6 next += 1; 7 await functions[index](); 8 } 9 }; 10 const size = Math.min(n, functions.length); 11 await Promise.all(Array.from({ length: size }, worker)); 12}
A shared next counter is safe without any locking because JavaScript is single-threaded: next += 1 cannot interleave. Math.min(n, functions.length) avoids spawning workers with nothing to do.
Time is the slowest possible packing of the tasks rather than their sum, and memory holds at most n in-flight promises however long the list is.
Goal: Run every task with at most n in flight, starting the next the moment a slot frees.
Promise.all starts everything at once; a pool starts n, and begins the next task only as a running one finishes.n workers that each pull the next unclaimed task keeps every slot busy and finishes measurably sooner.next += 1 cannot be interrupted halfway by another worker. This is one of the few places where the single-threaded model makes a concurrency problem simpler rather than harder.Continue learning with these related challenges
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 promises one after another and collect the results in order — built with .then chaining, no async/await.
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 promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6
Pratik Rai ·