bounded-concurrency
Promise.all(items.map(...)) or Promise.allSettled(items.map(...)) over a list whose length isn't fixed.
Why: map calls the callback for every item before Promise.all sees a single promise, so all the work starts at once. With ten items that's fine. With ten thousand it's ten thousand requests, open files or database queries at the same time: the server rate-limits or falls over, the process runs out of file descriptors (EMFILE) or sockets, and the connection pool queues everything anyway. The list is usually as long as the data, so the code that worked in testing fails in production. Guide: Async.
✕ Rejectedbounded-concurrency
ts
import { writeFile } from "node:fs/promises";
import { isErr, type Result, unsafeAsync } from "@evobug_com/safe-ts-std/result";
import type { SafePath } from "@evobug_com/safe-ts-std/path";
function save(path: SafePath, text: string): Promise<Result<void, unknown>> {
return unsafeAsync(() => writeFile(path, text));
}
export async function saveAll(files: ReadonlyMap<SafePath, string>): Promise<number> {
const results = await Promise.all(Array.from(files, ([path, text]) => save(path, text)));
return results.filter(isErr).length;
}✅ Accepted: at most 8 at a time
✓ Accepted0 diagnostics
ts
import { writeFile } from "node:fs/promises";
import { mapLimit } from "@evobug_com/safe-ts-std/async";
import { isErr, type Result, unsafeAsync } from "@evobug_com/safe-ts-std/result";
import type { SafePath } from "@evobug_com/safe-ts-std/path";
function save(path: SafePath, text: string): Promise<Result<void, unknown>> {
return unsafeAsync(() => writeFile(path, text));
}
export async function saveAll(files: ReadonlyMap<SafePath, string>): Promise<number> {
const results = await mapLimit([...files], 8, ([path, text]) => save(path, text));
if (isErr(results)) return files.size;
return results.filter(isErr).length;
}mapLimit(items, limit, fn, signal?) from @evobug_com/safe-ts-std/async resolves to the values in the order of items, or to an Err with the first failure; it never rejects. After a failure it starts nothing new and aborts the signal it gave the running calls, so they can stop. For Promise.allSettled (keep going, one outcome per item), make fn return a Result:
✓ Accepted0 diagnostics
ts
import { mapLimit } from "@evobug_com/safe-ts-std/async";
import { isErr, unsafeAsync } from "@evobug_com/safe-ts-std/result";
export async function countHealthy(hosts: readonly string[]): Promise<number> {
const outcomes = await mapLimit(hosts, 16, (host, _, signal) =>
unsafeAsync(() => fetch(`https://${host}/health`, { signal })),
);
// Only a bad limit or an aborted signal fails the whole run here.
if (isErr(outcomes)) return 0;
return outcomes.filter((response) => !isErr(response) && response.ok).length;
}A list whose length is written in the code is left alone: an array literal, a tuple, and the entries, keys or values of a table written out that can't get more keys (Object.freeze({ ... }), { ... } as const, without spreads), also through std's keysOf(table). So is a callback that only picks promises that already exist:
✓ Accepted0 diagnostics
ts
import { keysOf } from "@evobug_com/safe-ts-std/dict";
const SOUNDS = Object.freeze({ added: "/sounds/added.ogg", removed: "/sounds/removed.ogg" });
export async function both(user: Promise<string>, avatar: Promise<string>, jobs: readonly { done: Promise<void> }[]): Promise<void> {
await Promise.all([user, avatar].map((p) => p.then((text) => text.length)));
await Promise.all(jobs.map((job) => job.done));
}
async function cache(url: string): Promise<string> {
return `cached:${url}`;
}
export async function preload(): Promise<string[]> {
return Promise.all(Object.values(SOUNDS).map((url) => cache(url)));
}
export async function preloadNamed(): Promise<string[]> {
return Promise.all(keysOf(SOUNDS).map((name) => cache(SOUNDS[name])));
}ts
import { keysOf } from "@evobug_com/safe-ts-std/dict";
const SOUNDS = Object.freeze({ added: "/sounds/added.ogg", removed: "/sounds/removed.ogg" });
export async function both(user: Promise<string>, avatar: Promise<string>, jobs: readonly { done: Promise<void> }[]): Promise<void> {
{ const __la1 = [user, avatar]; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let p = __le1; __lr1[__li1] = (p.then((text) => text.length)); }} await Promise.all(__lr1); }
{ const __la2 = jobs; const __lr2: any[] = new Array(__la2.length); for (let __li2 = 0, __ln2 = __la2.length; __li2 < __ln2; __li2++) { if (!(__li2 in __la2)) continue; const __le2 = __la2[__li2]!; { let job = __le2; __lr2[__li2] = (job.done); }} await Promise.all(__lr2); }
}
async function cache(url: string): Promise<string> {
return `cached:${url}`;
}
export async function preload(): Promise<string[]> {
return Promise.all(Object.values(SOUNDS).map((url) => cache(url)));
}
export async function preloadNamed(): Promise<string[]> {
return Promise.all(keysOf(SOUNDS).map((name) => cache(SOUNDS[name])));
}What the rule can't see: how long the list really is. A list that is short by construction (the 3 tabs of a page, a chunk of 10) is still reported when its type doesn't say so: use mapLimit anyway, or a tuple type. Only Promise.all and Promise.allSettled directly around map or Array.from(list, fn) are checked; promises collected in a loop first (promises.push(upload(file))), settleAll(items.map(...)) and Promise.race/any are not.