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 ·
Wrap an async function so it gives up after a deadline — the building block behind every request timeout.
Write timeLimit(fn, t). It returns a wrapper that takes the same arguments as fn and settles the same way fn does — unless fn is still running after t milliseconds. In that case the wrapper gives up and rejects with "Timed out", a plain string rather than an Error.
Input:
JSfile.javascript1const lookup = timeLimit(async (word, ms) => { 2 await new Promise((r) => setTimeout(r, ms)); 3 return word.toUpperCase(); 4}, 200); 5 6lookup('fast', 80).then(console.log); 7lookup('slow', 350).catch(console.log);
Output:
FAST
Timed out
Both calls get the same 200ms budget. The first finishes at 80ms and its value passes straight through; the second would need 350ms, so it is rejected when the budget runs out at 200ms.
Every network call needs a deadline. Without one a hung request leaves a spinner on screen forever, because a promise that never settles never rejects either.
Two things are racing: the real call, and a timer that rejects. Promise.race says exactly that — whichever settles first wins, and the loser is ignored because a promise settles only once.
JSfile.javascript1function timeLimit(fn, t) { 2 return function (...args) { 3 return new Promise((resolve, reject) => { 4 const timer = setTimeout(() => reject('Timed out'), t); 5 fn.apply(this, args).then(resolve, reject).finally(() => clearTimeout(timer)); 6 }); 7 }; 8}
timeLimit returns a function, not a promise. Nothing starts until that function is called, which is what lets you wrap once and reuse.
Clearing the timer in finally matters more than it looks: a pending timer keeps the event loop alive and, in Node, delays process exit. It also means a rejection from fn itself passes straight through rather than being replaced by a timeout.
Goal: Reject with "Timed out" past the deadline, and pass everything else through untouched.
Promise.race settles with whichever finishes first, and because a promise settles only once the loser is ignored automatically.AbortController passed to fetch.finally covers both the success and failure paths.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 ·
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 ·
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 ·
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 ·