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
๐ฏ Goal: Execute all functions while maintaining at most
Key Requirements:
โข Execute functions in order:
โข Maintain exactly
โข When a promise resolves, immediately start the next available function
โข Return a promise that resolves when ALL functions complete
Example: If
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.
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 timeKey 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
nrepresents maximum concurrent promises
Visualization
Tap to expand
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code