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 ·
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 new function that takes the same arguments as fn and behaves identically, except that if fn has not settled within t milliseconds the returned promise rejects with the string "Time Limit Exceeded".
Input:
JSfile.javascript1const limited = timeLimit(async (n) => { 2 await new Promise((r) => setTimeout(r, 100)); 3 return n * 2; 4}, 50); 5limited(5).catch(console.log);
Output:
"Time Limit Exceeded"
The wrapped function needs 100ms but only has 50ms.
Goal: Reject with "Time Limit Exceeded" past the deadline, and pass everything else through untouched.
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 ·