Promise Pool With a Concurrency Limit
Hard · Async Patterns
Starting 1000 HTTP requests or file reads at once can exhaust sockets or file descriptors (EMFILE). Node.js code usually runs async work through a pool that keeps at most limit operations in flight (like the p-limit package).
Write async function promisePool(items, limit, worker):
- Call worker(item) for the items in input order, never having more than limit calls in flight.
- As soon as any call settles, start the next item (do not wait for a whole "batch" to finish).
- A rejection must not stop the pool. Resolve with an array in input order, like Promise.allSettled: { status: "fulfilled", value } or { status: "rejected", reason }.
- If limit is not a positive integer, reject with a RangeError (before calling the worker).
- An empty items array resolves to [].
The driver runPool(tasks, limit) is already written. Each task is { id, ms, fail? }; its worker "works" for ms milliseconds of simulated time (a fake clock), then returns id.toUpperCase() or throws Error(id + " failed"). The driver returns { results, maxInFlight, log }, where rejection reasons are turned into their messages and log shows when each task started and ended. If your pool rejects, it returns { error: errorName }.
Next: Safe Static File Server