Promise Pool - Problem

Given an array of asynchronous functions functions and a pool limit n, return an asynchronous function promisePool. It should return a promise that resolves when all the input functions resolve.

Pool limit is defined as the maximum number promises that can be pending at once. promisePool should begin execution of as many functions as possible and continue executing new functions when old promises resolve.

promisePool should execute functions[i] then functions[i + 1] then functions[i + 2], etc. When the last promise resolves, promisePool should also resolve.

For example, if n = 1, promisePool will execute one function at a time in series. However, if n = 2, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.

You can assume all functions never reject. It is acceptable for promisePool to return a promise that resolves any value.

Input & Output

Example 1 — Pool Limit 2
$ Input: functions = [() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))], n = 2
Output: Promise that resolves after ~500ms
💡 Note: First 2 functions start immediately. Function 1 (300ms) and function 2 (400ms) run concurrently. After 300ms, function 1 completes and function 3 (200ms) starts. Total time: max(300, 400) + 200 = 500ms.
Example 2 — Pool Limit 1
$ Input: functions = [() => new Promise(res => setTimeout(res, 100)), () => new Promise(res => setTimeout(res, 200))], n = 1
Output: Promise that resolves after 300ms
💡 Note: With n=1, functions execute sequentially: first function (100ms), then second function (200ms). Total: 100 + 200 = 300ms.
Example 3 — Empty Array
$ Input: functions = [], n = 3
Output: Promise that resolves immediately
💡 Note: No functions to execute, so the pool resolves immediately.

Constraints

  • 0 ≤ functions.length ≤ 10
  • 1 ≤ n ≤ 10

Visualization

Tap to expand
Promise Pool with Queue INPUT functions = [fn0, fn1, fn2] fn0: setTimeout(300ms) 0 fn1: setTimeout(400ms) 1 fn2: setTimeout(200ms) 2 Pool Limit n = 2 Max 2 concurrent promises at any time ALGORITHM STEPS 1 Start Pool Begin n concurrent workers 2 Execute in Order Pick next function from queue 3 On Resolve Start next function (if any) 4 Complete Resolve when all done Timeline (ms) 0 200 300 500 fn0 (300ms) fn1 (400ms) fn2 W1: W2: W1: W = Worker slot FINAL RESULT Promise Resolves ~500ms Execution Summary t=0: fn0, fn1 start t=300: fn0 done, fn2 starts t=400: fn1 done t=500: fn2 done [OK] All Resolved Pool Complete Max 2 running at once maintained throughout Key Insight: The Promise Pool pattern uses a worker queue where each worker recursively picks the next task upon completion. With n=2, we maintain 2 concurrent executions. When fn0 finishes at 300ms, fn2 immediately starts. The pool resolves when all n workers have no more tasks --> ~500ms total. TutorialsPoint - Promise Pool | Queue-based Concurrency Control
Asked in
Meta 25 Google 20
12.0K Views
Medium Frequency
~25 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen