Skip to main content
btheo.com btheo.com > press start to play
NEW POST: NODE.JS SECURITY 2025 OPEN FOR FREELANCE 10+ YEARS EXP REACT × NODE × AWS NEW POST: NODE.JS SECURITY 2025 OPEN FOR FREELANCE 10+ YEARS EXP REACT × NODE × AWS
TIL · 21 JAN 2026 · NOTE #021 ESC
TIL NOTE #021

Promise.all Is Not Free

The problem: You have 100 items. You run Promise.all with 100 queries. Your pool has 10 connections. 90 queries queue. Timeout.

// ✗ BAD: fires 100 queries at once
const users = await Promise.all(
userIds.map(id => db.query("SELECT * FROM users WHERE id = ?", [id]))
);

The fix: Batch with a chunk helper.

function chunk<T>(array: T[], size: number): T[][] {
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) =>
array.slice(i * size, (i + 1) * size)
);
}
// Process 10 at a time
const batches = chunk(userIds, 10);
const results = [];
for (const batch of batches) {
results.push(
...(await Promise.all(batch.map(id => db.query(...))))
);
}

Pool stays at ~10 concurrent. Everything finishes.

Independent I/O (different databases, external APIs)? Use Promise.all. But if they share a resource (connection pool, rate limit), respect the ceiling.