Run async tasks with a concurrency limit
Process a large list with at most N promises in flight at once, preserving result order.
- JavaScript
- GreenSnippets
- MIT
- Executed with Node.js 22
async function mapPool(items, limit, worker) {
const results = new Array(items.length);
let next = 0;
async function run() {
while (next < items.length) {
const index = next++;
results[index] = await worker(items[index], index);
}
}
const workers = Array.from({ length: Math.min(limit, items.length) }, run);
await Promise.all(workers);
return results;
}Example / expected output
await mapPool(urls, 5, (url) => fetch(url).then((r) => r.status));How it works
Promise.all on thousands of items fires them all at once, which can exhaust sockets or hit rate limits. This pool starts limit workers that pull the next item from a shared index. Results keep the input order, and the first error rejects the whole call.
Related snippets
- JavaScript Tested
Sleep / delay with a Promise
Pause an async function for a number of milliseconds, with optional cancellation through AbortSignal.
- JavaScript Tested
Retry an async operation with exponential backoff
Retry a failing promise-returning function with growing, jittered delays and a retry predicate.
- Python Tested
Retry decorator with exponential backoff
Re-run a flaky function a limited number of times with growing delays, only for chosen exception types.
- JavaScript Tested
Fetch every page of a paginated API with an async generator
Iterate cursor-paginated API results with for await, loading pages lazily and stopping early when needed.

