forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

map-concurrent.ts28 lines, 1.0 KiBHistoryRawDownload
1/**
2 * `Promise.all(items.map(fn))` with at most `limit` calls in flight. For the
3 * per-row fan-outs the backend has no bulk read for (marks, balances, member
4 * counts): an unbounded burst of a few hundred GETs trips the backend's rate
5 * limit and stalls every other request the page makes behind it.
6 *
7 * Results keep the input order. A rejection rejects the whole call, like
8 * `Promise.all` — callers that tolerate a failed row catch inside `fn`.
9 */
10export async function mapConcurrent<T, R>(
11 items: readonly T[],
12 limit: number,
13 fn: (item: T, index: number) => Promise<R>,
14): Promise<R[]> {
15 const results = new Array<R>(items.length);
16 let next = 0;
17 const worker = async () => {
18 while (next < items.length) {
19 const index = next++;
20 results[index] = await fn(items[index], index);
21 }
22 };
23 await Promise.all(Array.from({ length: Math.min(Math.max(1, limit), items.length) }, worker));
24 return results;
25}
26
27/** In-flight cap shared by the page-level fan-outs. */
28export const FAN_OUT_LIMIT = 6;