mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-08-28 08:35:57 +00:00
22 lines
722 B
TypeScript
22 lines
722 B
TypeScript
/**
|
|
* Memoize an async factory, but drop a rejected result so the next call
|
|
* retries.
|
|
*
|
|
* On success the resolved promise is cached and shared across all callers (the
|
|
* factory runs once). On failure the cache is cleared before the rejection is
|
|
* re-thrown, so a transient init failure does not poison the memo for the
|
|
* process lifetime — the next call re-invokes the factory.
|
|
*/
|
|
export function memoizeAsyncWithRetry<T>(
|
|
factory: () => Promise<T>,
|
|
): () => Promise<T> {
|
|
let cached: Promise<T> | null = null;
|
|
return () => {
|
|
cached ??= factory().catch((error: unknown) => {
|
|
cached = null; // poisoned result cleared → next call re-attempts
|
|
throw error;
|
|
});
|
|
return cached;
|
|
};
|
|
}
|