Files
vue-vben-admin/packages/hooks/src/useRequest/utils/cachePromise.ts

24 lines
655 B
TypeScript

type CachedKey = string | number;
const cachePromise = new Map<CachedKey, Promise<any>>();
export const getCachePromise = (cacheKey: CachedKey) => {
return cachePromise.get(cacheKey);
};
export const setCachePromise = (cacheKey: CachedKey, promise: Promise<any>) => {
// Should cache the same promise, cannot be promise.finally
// Because the promise.finally will change the reference of the promise
cachePromise.set(cacheKey, promise);
// no use promise.finally for compatibility
promise
.then((res) => {
cachePromise.delete(cacheKey);
return res;
})
.catch(() => {
cachePromise.delete(cacheKey);
});
};