Bounded concurrency for many async requests
limiting concurrency, not just running parallel.
chunk the array and await Promise.all per chunk, or run a fixed worker pool pulling from a shared index; cap in-flight requests.
firing all 1000 at once or going fully serial.
WHAT THIS TESTS This probes whether you can implement rate-limited concurrency rather than defaulting to all-at-once or one-at-a-time. It is a common real-world scaling concern.
A GOOD ANSWER COVERS Firing all thousand requests with a single Promise.all opens a thousand sockets at once, which can exhaust file descriptors, trip rate limits, spike memory, and hammer the remote server. A fully serial loop avoids that but wastes time by never overlapping. The goal is a fixed concurrency limit. The simplest correct design splits the URL list into chunks of the desired size and awaits Promise.all on each chunk before moving to the next; concurrency is bounded but the batch only advances when its slowest member finishes. A more efficient design uses a worker pool: spawn N workers that each loop, atomically taking the next index from a shared counter and processing that URL, until the array is exhausted. This keeps exactly N requests in flight at all times, so a slow request does not stall the others. Either way you collect results in order and decide whether to fail fast or capture per-item errors.
COMMON WRONG ANSWERS Picking only the two extremes, forgetting to preserve result ordering, mutating a shared index without care so URLs are skipped or duplicated, or using allSettled when you actually need to stop on error. Another miss is not handling individual fetch failures within a batch.
LIKELY FOLLOW-UPS Why the worker-pool keeps utilization higher than fixed chunks, how to preserve input order in the results, how to add retries and timeouts, and how libraries like p-limit implement this.
ONE CONCRETE EXAMPLE With a limit of ten and a worker pool, you start ten workers; each grabs index zero through nine, and as soon as any worker finishes, it grabs index ten, then eleven, and so on. At any instant exactly ten fetches are active until the tail of the list, so total time approximates the slowest path rather than the sum, and the remote server never sees more than ten concurrent connections from you.
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.