
Learn why wrapping everything in Promise.all crashes Node.js APIs and how to handle concurrency safely with rate limiting and batching.
When you are building a modern web application, handling multiple tasks at the same time feels like a superpower. Whether you are fetching user profiles, processing webhook events, or querying a database, JavaScript makes it incredibly easy to kick off asynchronous operations. Very often, the first tool developers reach for is Promise.all. It is built into the language, clean to read, and lets you run tasks in parallel.
However, in my experience building full-stack applications and high-traffic APIs for clients, I have seen Promise.all cause major outages when misused. The culprit is not the function itself, but unbounded concurrency. When you pass an array of hundreds or thousands of promises to Promise.all, Node.js tries to execute all of them at the exact same moment. This can overwhelm databases, exhaust connection pools, hit external rate limits, and crash your server.
In this post, I want to break down why unbounded concurrency happens, how it affects your backend infrastructure, and how you can manage asynchronous workloads safely.
The Trap of Unbounded Concurrency
Let us look at a common scenario. Imagine you have a backend endpoint in a Node.js API that needs to fetch details for a list of items or update status flags for every user in a large database table. You might write something like this:
// Dangerous pattern: Unbounded concurrency
const results = await Promise.all(
items.map(async (item) => {
return await externalApiCall(item.id);
})
);
If items contains 20 elements, this code usually runs fine. But what happens when your platform grows, and items suddenly contains 5,000 records?
Behind the scenes, your application fires off 5,000 concurrent network requests or database queries simultaneously. Your server's event loop gets hammered, memory spikes as thousands of request objects stay alive in memory, and external APIs will immediately block your IP with 429 Too Many Requests errors.
How This Impacts Your Backend
When building custom software like JobsiteX or managing data-heavy platforms, stability is everything. Unbounded concurrency introduces three major failure points:
Limiting Concurrency Safely
To solve this, you do not need to abandon parallel execution entirely. You just need to control how many operations run at the exact same time. This is known as concurrency batching or throttling.
Instead of firing everything at once, you can process items in chunks. Here is a simple utility function I often use in my backend projects to process arrays in batches:
async function promisePool(items, limit, fn) {
const results = [];
const executing = [];
for (const item of items) {
const p = Promise.resolve().then(() => fn(item));
results.push(p);
if (limit <= items.length) {
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= limit) {
await Promise.race(executing);
}
}
}
return Promise.all(results);
}
Alternatively, if you prefer using battle-tested libraries, packages like p-limit or async provide robust concurrency control with very little overhead.
import pLimit from 'p-limit';
const limit = pLimit(5); // Allow only 5 concurrent operations
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const tasks = input.map(i => {
return limit(() => fetchSomething(i));
});
const results = await Promise.all(tasks);
By restricting execution to a safe threshold (like 5 or 10 concurrent requests depending on your server capacity), your application processes the entire queue smoothly without choking the CPU or database.
Best Practices for Production APIs
When designing scalable architectures, keep these guardrails in mind:
.map() loops: Search your codebase for Promise.all(array.map(...)) and check if the input array size can grow dynamically.Conclusion
JavaScript makes asynchronous programming feel effortless, but with great power comes the responsibility to protect your infrastructure. By replacing unbounded Promise.all calls with controlled concurrency limits, you can save your APIs from sudden crashes and keep your applications running reliably under heavy loads.
If you are scaling up your backend infrastructure, optimizing your API performance, or building a robust web application, get in touch and let us build it right.
Tagged with
Leave a comment