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 ·
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
getPromiseByIndex(i) returns the promise for index i, or null when there are none left.
Write promiseAllSync(getPromiseByIndex, count) that resolves each one in order — the next must not start until the previous has resolved — and resolves with an array of the values.
Build the chain with .then(), without async/await — chaining by hand is the point of the exercise.
Input:
JSfile.javascript1const get = (i) => (i < 3 2 ? new Promise((r) => setTimeout(() => r(i * 10), 100)) 3 : null); 4 5await promiseAllSync(get, 3);
Output:
[0, 10, 20] // after ~300ms
Sequential, so the three 100ms waits add up. Promise.all would have taken 100ms and defeated the point.
chain variable inside a loop is the idiom: chain = chain.then(...).Calling every getter up front starts every promise immediately, and no amount of awaiting afterwards makes them sequential — the work is already in flight. Sequential means the next call happens inside the previous .then.
One chain variable, reassigned each iteration. The loop builds the chain; nothing runs until the previous link resolves.
JSfile.javascript1function promiseAllSync(getPromiseByIndex, count) { 2 const results = []; 3 let chain = Promise.resolve(); 4 for (let i = 0; i < count; i += 1) { 5 chain = chain.then(() => { 6 const promise = getPromiseByIndex(i); 7 if (promise === null || promise === undefined) return undefined; 8 return promise.then((value) => { results.push(value); }); 9 }); 10 } 11 return chain.then(() => results); 12}
The exercise rules out async/await deliberately: written as a fold over promises, it is obvious that each link creates its promise only when reached. The async version reads more naturally but hides that.
A rejection anywhere breaks the chain and propagates, so the result rejects — matching Promise.all's fail-fast behaviour even though nothing runs in parallel.
Goal: Resolve every promise in order using .then chaining, and return the values as an array.
Promise.resolve() and reassign it each iteration with chain = chain.then(() => next()). The loop builds the chain; each link creates its promise only when reached.async version hides the mechanism. Written as a fold over .then, it is obvious that the next promise is created inside the previous callback, which is the entire reason it runs sequentially.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 ·