Throttle Promises

Hard
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Throttle Promises

Approach

Maintain a running count and a queue index. Launch tasks up to the concurrency limit. When each task completes, decrement the running count and launch the next queued task. Resolve the outer promise when all tasks finish and none are running.

Complexity Analysis

Time
O(n)
Space
O(n)

Pattern

Promise Pool

Why It Works

The runNext function acts as a semaphore, only allowing max concurrent tasks at any time while draining the queue as slots free up.

Updated Feb 2026