@daisugi/kintsugi
Version:
Kintsugi is a set of utilities to help build a fault tolerant services.
22 lines • 821 B
JavaScript
import { SimpleMemoryStore } from './simple_memory_store.js';
import { stringifyArgs } from './stringify_args.js';
export function reusePromise(fn) {
const simpleMemoryStore = new SimpleMemoryStore();
return async function (...args) {
const cacheKey = stringifyArgs(args);
const cacheResponse = simpleMemoryStore.get(cacheKey);
if (cacheResponse.isSuccess) {
return cacheResponse.getValue();
}
const response = fn.apply(this, args).then((value) => {
simpleMemoryStore.delete(cacheKey);
return value;
}, (reason) => {
simpleMemoryStore.delete(cacheKey);
throw reason;
});
simpleMemoryStore.set(cacheKey, response);
return response;
};
}
//# sourceMappingURL=reuse_promise.js.map