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 onceconst 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 timeconst 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.