const PromiseCache = new Map>(); export async function CachedPromise(key: string, promise: Promise): Promise { const cache = PromiseCache.get(key); if (cache) { return cache; } PromiseCache.set(key, promise); return promise.finally(() => PromiseCache.delete(key)); } type PromiseFunc = () => Promise; export async function CachedPromiseFunc(key: string, promiseFunc: PromiseFunc): Promise { const cache = PromiseCache.get(key); if (cache) { return cache; } const promise = promiseFunc(); PromiseCache.set(key, promise); return promise.finally(() => PromiseCache.delete(key)); }