ITADN

feat: coalesce (single-flight) utility

#1608Openluisbytes 创建于 2026-02-07
L
luisbytescommented
Hello, I would like a coalition utility to group identical concurrent asynchronous calls into a single execution. Most implementations have a re-entrancy bug where the factory runs before the promise is stored. A robust version for the toolkit would be: ```typescript const inflight = new Map<string, Promise<any>>(); export async function coalesce<T>(key: string, fn: () => T | PromiseLike<T>): Promise<T> { const active = inflight.get(key); if (active) return active; const promise = Promise.resolve() .then(() => fn()) .finally(() => inflight.delete(key)); inflight.set(key, promise); return promise; } ```
0 条评论