Promise Time Limit
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.javascript1promisePool([f1, f2, f3], 2) // f1: 300ms, f2: 400ms, f3: 200ms
Output:
resolves after ~500ms
f1 and f2 start together. f1 finishes at 300ms, freeing a slot for f3, which finishes at 500ms. Batching would have taken 600ms.
Goal: Run every task with at most n in flight, starting the next the moment a slot frees.
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 ·