Run tasks with bounded concurrency
Firing every request at once exhausts the browser's connection pool and can get you rate-limited; running them one by one wastes minutes. The middle ground is a pool that keeps a fixed number in flight.
Implement pool(tasks, limit), where tasks is an array of zero-argument functions returning promises. Start the first limit tasks immediately; each time one fulfils, start the next task that has not run yet. Resolve with an array of the results in the original task order, not completion order. Never have more than limit tasks running at once. An empty array resolves with an empty array. If a task rejects, reject with that reason and do not start any task that has not already begun.
What it has to do
- Never run more than
limittasks at the same time. - Start a waiting task as soon as a running one finishes.
- Resolve with results in the original task order.
- Resolve with
[]for an empty task list. - Reject on the first rejection and start no further tasks.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.