@daysnap/utils
Version:
23 lines (21 loc) • 407 B
JavaScript
// src/withCache.ts
function withCache(fn) {
const cache = {};
return function(...args) {
const key = JSON.stringify(args);
const hit = cache[key];
if (hit) {
return hit;
}
const res = fn(...args);
if (res instanceof Promise) {
return res.then((res2) => {
return cache[key] = res2;
});
}
return cache[key] = res;
};
}
export {
withCache
};