At 02:14 UTC, a scheduled billing job ran await Promise.all(invoices.map(syncInvoice)) across 1,200 customer records. Within 400 milliseconds, the payment gateway returned 429 Too Many Requests, PostgreSQL threw connection pool timeouts, and 1,199 completed writes became orphaned because the final promise rejected.
Most JavaScript and TypeScript developers reach for Promise.all whenever an array of async tasks needs to run faster, assuming the runtime will schedule those tasks responsibly. It will not.
How Array.map() Turns Promise.all Into a Traffic Spike
When replacing a slow sequential for...of loop, the standard reflex is mapping an array of IDs directly into Promise.all(ids.map(fetchUser)). In local development with 5 mock records, this pattern looks clean and passes every test. In production with 5,000 records, it becomes a self-inflicted denial-of-service attack.
The bug stems from confusing synchronization with scheduling. Promise.all does not throttle or queue execution. Because Array.prototype.map() is synchronous, Node.js invokes every async callback on the exact same tick of the event loop. Before Promise.all even receives the array of promises, the runtime has already initiated 5,000 concurrent DNS lookups, TLS handshakes, socket buffers, and database queries.
Three Ways Unbounded Concurrency Breaks Production
In my production audits, firing unbounded promise arrays consistently triggers three cascading failure modes:
- Downstream Thundering Herd: External APIs enforce strict rate limits, and relational databases typically cap connection pools between 10 and 50 connections. Opening 1,000 concurrent requests saturates the pool immediately and starves unrelated endpoints.
-
Fail-Fast Orphaned Side Effects:
Promise.allrejects the instant one promise fails, but JavaScript promises do not cancel automatically. The remaining 999 HTTP calls and database mutations keep mutating state in the background with nothing awaiting their completion. - V8 Heap and GC Stalls: Buffering thousands of raw response streams and parsed JSON payloads simultaneously causes sharp memory spikes, triggering stop-the-world garbage collection pauses or container OOM kills.
Capping Active Promises With a Bounded Worker Pool
To measure the difference in a reproducible benchmark, I ran 200 simulated 50ms requests against a downstream service capped at 10 concurrent connections (returning HTTP 429 above 10). Unbounded Promise.all crashed in 52ms with zero completed tasks, while a bounded worker pool finished all 200 tasks in 1,045msβnearly 10x faster than a sequential loop without dropping a single request.
Instead of firing every task at once, spin up N lightweight workers that pull from a shared index and wire an AbortController to stop queued work on fatal errors:
export async function mapWithConcurrency<T, R>(
items: readonly T[],
limit: number,
workerFn: (item: T, signal: AbortSignal) => Promise<R>
): Promise<R[]> {
const results = new Array<R>(items.length);
const controller = new AbortController();
let cursor = 0;
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (!controller.signal.aborted) {
const index = cursor++;
if (index >= items.length) break;
try {
results[index] = await workerFn(items[index], controller.signal);
} catch (err) {
controller.abort(err);
throw err;
}
}
});
await Promise.all(workers);
return results;
}
When syncing independent records where one failure should not abort the entire batch, catch errors inside workerFn and return { status: "fulfilled" | "rejected" } objectsβmirroring Promise.allSettled while still honoring the concurrency ceiling.
Why Splitting Arrays Into Fixed Chunks Still Wastes Throughput
Slicing an array into chunks of 10 and awaiting Promise.all(chunk) in a loop prevents 429 errors, but introduces head-of-line blocking: 9 fast 20ms requests sit idle waiting for 1 slow 900ms outlier before the next batch begins. A shared worker pool keeps all 10 slots saturated continuously until the queue drains.
Choosing the Right Concurrency Strategy
| Strategy | Max In-Flight | 200-Task Benchmark | Failure Behavior | Best Use Case |
|---|---|---|---|---|
Promise.all(items.map(...)) |
Unbounded (N) |
Failed at ~52 ms (HTTP 429) |
Fail-fast without aborting in-flight work | Fixed tuple of 2β5 independent calls |
Promise.allSettled(items.map(...)) |
Unbounded (N) |
Spiked to 200 connections (HTTP 429) |
Waits for all tasks; still floods downstream | Small, fixed tuple tolerating partial errors |
Sequential for...of
|
1 | 10,180 ms (100% succeeded) | Halts on first error | Strict ordering or fragile single-threaded APIs |
Bounded Worker Pool (limit = 10) |
10 |
1,045 ms (100% succeeded) | Aborts remaining queue via AbortSignal
|
Dynamic arrays, DB batches, external API syncs |
The Takeaway
Promise.all works well for awaiting a small, known tuple of heterogeneous callsβsuch as loading a user profile, permissions, and feature flags during an API request. The moment the input becomes a dynamic array of unknown length, wrapping .map() in Promise.all turns into a reliability hazard. Cap concurrency explicitly so throughput stays high without overwhelming downstream services.
What failure mode have you seen in production when an unbounded Promise.all hit a database pool or rate-limited API?
Connect with Me
If you found this article helpful, let's connect!
- π» GitHub: johnnylemonny
Open to opportunities. Looking for commisions and collaborations.

Top comments (34)
This is a really good distinction: synchronization is not scheduling. π
I think thatβs where
Promise.all(items.map(...))becomes deceptive. The code looks nicely controlled because everything is wrapped in oneawait, but the actual work may already be flooding the downstream service.I also really like the comparison with fixed-size chunks. A worker pool keeping the available slots busy is much nicer than making 9 fast tasks wait for one slow one. πΈ
Great article, John! π
Thanks so much, nyaomaru! That single
awaitreally does create a false sense of safety. It looks tidy on the page even while all those requests are already firing at once. Glad the chunking comparison resonated too, since eliminating that tail-latency bottleneck is usually where the biggest real-world speedup comes from.One thing worth adding to the numbers: swapping in Promise.allSettled fixes nothing here. It changes which rejections you see, not how many sockets you open, and one 429 still poisons the other 1,199 writes. The unbounded part is map() itself, so bounding the pool (p-limit, or an index-based worker loop over a fixed array of runners) is the actual fix; allSettled sits on top of that if you want per-invoice results instead of fail-fast.
Second: fetch won't queue for you either. undici's default Agent has no per-origin connection cap, so an app-level limit of ~8 is the only thing between the billing job and the payment gateway β same reason keep-alive alone doesn't save you. And even a bounded pool leaves partial commits when the gateway rejects mid-run, so the durable part is an idempotency key per invoice so a re-run doesn't double-charge. Did you reconcile the 1,199 orphaned writes by hand, or add a resume cursor keyed on the last committed id?
Great points across the board, especially calling out
undiciβs defaultAgentbehavior, since a lot of folks assume keep-alive automatically queues excess requests per origin. For that initial incident, I reconciled the completed writes against the gateway's settlement logs and immediately paired the bounded pool with deterministic per-invoice idempotency keys and a persisted sync status so retries safely skip committed records. Appreciate you adding that production depth to the thread.Small trap in mapWithConcurrency. If limit comes from config and ends up 0 or NaN (
Number(process.env.SYNC_CONCURRENCY)when the var isn't set), Array.from makes zero workers, workerFn never runs, and it resolves with 5 empty slots like the job succeeded. I'd throw unless limit is an integer >= 1.Great catch. That is a nasty silent-failure trap, especially when
Number(process.env.SYNC_CONCURRENCY)evaluates toNaNon an unset env var. SinceMath.min(NaN, items.length)resolves toNaN,Array.fromquietly creates zero workers and returns sparse empty slots as if the whole batch succeeded. Adding an explicitNumber.isInteger(limit) && limit >= 1guard at the top is definitely worth the extra line in production code.The 429 storm gets all the attention, but the orphaned side effects are the part that scared me. Promise.all rejects on the first failure while the other 999 calls keep mutating state in the background with nothing awaiting them. I've hit the same shape with webhook retries, where the first attempt already landed and the retry has no idea. In your worker pool, when the AbortController fires mid-batch, what happens to the result slots for work that never ran? Do they stay empty, and can the caller tell "never ran" apart from "failed"?
Orphaned writes on webhook retries are brutal to untangle after the fact. In the fail-fast
mapWithConcurrencysnippet, rethrowing inside the worker causesPromise.all(workers)to reject directly, so theresultsarray is never returned and any unstarted slots simply remain uninitialized holes. When I need full per-item visibility after an early abort, I pre-fillresultswith{ status: "skipped" }before starting the workers so the caller can cleanly distinguish tasks that failed from ones that never ran. Thanks for bringing up that edge case.The pre-fill is the clean version β I was expecting holes to be the price of fail-fast, and skipped slots just remove the question entirely. Going to try that shape, see what the caller actually gets back after an abort. Does anything downstream ever mistake a skipped slot for a real result, or is the status field enough to keep them apart?
Great breakdown. The orphaned-writes point is the one that bites hardest, because the 429s show up in logs but the half-finished writes don't. Have you compared p-limit or p-map against a hand-rolled batching queue? I'm curious how you pick the concurrency number, whether from the upstream rate limit or from load testing.
In terms of raw throughput,
p-limitandp-mapperform virtually identically to a hand-rolled worker loop since they all just coordinate promise queues on the event loop. I usually reach forp-mapin production apps because it handlesAbortSignalandstopOnErrorout of the box, and keep the hand-rolled loop for zero-dependency scripts. To pick the concurrency limit, I start from the tightest hard constraint (such as the DB connection pool size or documented upstream limits with a 20β30% safety buffer) and tune upward in staging load tests until p95 latency starts climbing. Thanks for the thoughtful question.Starting from the tightest hard limit with a buffer, then letting p95 in staging tell you when to stop, is a really practical rule. Good point on p-map's AbortSignal and stopOnError too; that alone saves re-implementing cancellation in a hand-rolled loop. Thanks for the detailed answer.
The 02:14 UTC 1,200-invoice failure is such a textbook trapβPromise.all looks harmless on 5 local mocks, then fires 5,000 sockets on the exact same tick in prod because map() doesn't throttle anything. Pairing bounded concurrency with idempotent partial state saves so much cleanup.
That local-to-prod jump is why that pattern catches so many teams off guard. Everything looks calm with five mocks until a real batch opens a flood of sockets on a single tick. Pairing a concurrency cap with idempotent state tracking makes recovery after a partial run vastly cleaner.
A concurrency limit is one of those fixes that looks boring and prevents a whole class of outages. A small p-limit around the batch usually costs a few lines and saves a rate-limit storm.
Spot on. Those unglamorous 5-line guardrails are usually the difference between a quiet on-call rotation and a 2 AM rate-limit cascade, and
p-limitis a great lightweight default for that exact reason. Thanks for reading.One extra edge case if this job runs across multiple replicas: a worker pool only limits concurrency inside one process. Two overlapping billing runs or several workers can each stay under their local cap while the payment gateway still sees a burst above the account-level rate limit.
For shared API keys, I'd pair local concurrency limits with a distributed rate budget and make sure scheduled runs can't overlap when that's unsafe.
Great addition, Vlad. An in-process pool only protects a single instance, so horizontal scaling or overlapping cron triggers can quietly multiply traffic against a shared API key. Putting a distributed lock around scheduled runs and coordinating global throughput via a shared rate limiter (like a Redis token bucket) closes that multi-replica gap cleanly.
Promise.all over a 10k-item array is basically a DDoS you schedule against your own database.
The bounded worker pool is the right fix. It's the same idea as backpressure in distributed systems, just at the function level. If the downstream API is rate-limited, pairing the pool with a simple token bucket keeps you from tripping 429s in bursts too.
Rate limiting and backpressure get a full deep dive in Grokking the System Design Interview at DesignGurus, and this is honestly one of the cleanest small-scale examples of why it matters that I've seen.
The worker pool is the part I'd actually copy, but one thing β aborting the controller stops the queue, it doesn't cancel the in-flight calls unless workerFn listens to the signal, so for that billing job you'd still have a handful of orphaned writes, just bounded instead of 1,199. Do you pass the signal into the actual gateway call in practice, or is stopping the queue good enough because the pool is small?
Great catch. In practice, I always pass
signaldirectly intofetchor the HTTP client to release sockets immediately, while keeping in mind that a client-side abort won't roll back a mutating gateway request the server has already received. Bounding the pool shrinks that blast radius to just a handful of records, and pairing it with per-invoice idempotency keys and persisted sync statuses handles those few in-flight writes cleanly on retry. Thanks for highlighting that nuance.