Promise Pool - Problem
Promise Pool is a critical concurrency control pattern in JavaScript that manages the execution of asynchronous operations with a limited pool of concurrent promises.

You're given an array of functions (asynchronous functions that return promises) and a pool limit n. Your task is to implement a promisePool function that:

๐ŸŽฏ Goal: Execute all functions while maintaining at most n concurrent promises at any time

Key Requirements:
โ€ข Execute functions in order: functions[0], then functions[1], then functions[2], etc.
โ€ข Maintain exactly n concurrent promises (when possible)
โ€ข When a promise resolves, immediately start the next available function
โ€ข Return a promise that resolves when ALL functions complete

Example: If n = 2 and you have 5 functions:
1. Start functions[0] and functions[1] simultaneously
2. When either completes, start functions[2]
3. When another completes, start functions[3]
4. Continue until all functions are executed and resolved

This pattern is essential for rate limiting API calls, managing database connections, and controlling resource usage in production applications.

Input & Output

example_1.js โ€” Basic Pool Limit
$ Input: functions = [f1, f2, f3, f4, f5], n = 2 where f1 takes 300ms, f2 takes 400ms, f3 takes 200ms, etc.
โ€บ Output: All functions execute with max 2 concurrent at any time Execution order: f1,f2 start โ†’ f3 starts when f1 ends โ†’ f4 starts when f3 ends โ†’ f5 starts when f2 ends
๐Ÿ’ก Note: With n=2, we maintain exactly 2 concurrent promises. As soon as one completes, the next function in the queue starts immediately, ensuring optimal resource utilization.
example_2.js โ€” Pool Larger Than Functions
$ Input: functions = [f1, f2], n = 5
โ€บ Output: Both functions execute immediately since n > functions.length
๐Ÿ’ก Note: When the pool limit exceeds the number of functions, all functions can run concurrently. The pool limit acts as an upper bound.
example_3.js โ€” Single Execution Pool
$ Input: functions = [f1, f2, f3, f4], n = 1
โ€บ Output: Functions execute sequentially: f1 โ†’ f2 โ†’ f3 โ†’ f4
๐Ÿ’ก Note: With n=1, functions execute in strict sequential order. This is useful for scenarios requiring serialized execution while maintaining the promise-based interface.

Constraints

  • 1 โ‰ค functions.length โ‰ค 104
  • 1 โ‰ค n โ‰ค 103
  • All functions return promises that never reject
  • Functions must be executed in the given order
  • The pool limit n represents maximum concurrent promises

Visualization

Tap to expand
Promise Pool Timeline (n=2, 5 functions)Time โ†’0ms100ms200ms300ms400msSlot 1:Function 1 (100ms)Function 3 (80ms)Function 5 (120ms)Slot 2:Function 2 (160ms)Function 4 (100ms)Events:F1 ends โ†’ F3 startsF2 ends โ†’ F4 startsF3 ends โ†’ F5 startsF4 endsF5 ends โ†’ All complete!Key Observations:โœ“ Never more than 2 functions running simultaneouslyโœ“ New function starts immediately when slot becomes availableโœ“ Total time: 300ms (vs 470ms sequential, vs potential resource overload parallel)โœ“ Perfect balance between efficiency and resource control
Understanding the Visualization
1
Initialize
Start first n functions up to pool limit
2
Monitor
Wait for any promise to complete using Promise.race()
3
Replace
Immediately start next function when slot becomes available
4
Complete
Continue until all functions are executed and resolved
Key Takeaway
๐ŸŽฏ Key Insight: The promise pool pattern achieves optimal resource utilization by maintaining a constant number of active promises, replacing completed ones immediately to maximize throughput while respecting system constraints.
Asked in
Meta 45 Google 38 Netflix 32 Amazon 28 Microsoft 25
42.8K Views
High Frequency
~25 min Avg. Time
1.5K 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