From 4ee9a51e226c670f398af15c21819f9b994b6b25 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 19 Dec 2024 21:18:15 +0900 Subject: [PATCH] fix: improve error handling --- src/memoize.ts | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/memoize.ts b/src/memoize.ts index dd871f2..fd2a8d7 100644 --- a/src/memoize.ts +++ b/src/memoize.ts @@ -112,18 +112,22 @@ export function memoizeFactory({ } } cache.set(key, [result, now]); - try { - if (result instanceof Promise) { - void (async () => { - const promise = persistCache?.(key, now, await result) as unknown; + if (result instanceof Promise) { + void (async () => { + try { + const promise = persistCache?.(key, now, result) as unknown; if (promise instanceof Promise) promise.catch(noop); - }); - } else { + } catch { + // do nothing. + } + }); + } else { + try { const promise = persistCache?.(key, now, result) as unknown; if (promise instanceof Promise) promise.catch(noop); + } catch { + // do nothing. } - } catch { - // do nothing. } console.log(`Exiting getter ${String(context.name)}.`); @@ -189,18 +193,22 @@ export function memoizeFactory({ } } cache.set(key, [result, now]); - try { - if (result instanceof Promise) { - void (async () => { - const promise = persistCache?.(key, now, await result) as unknown; + if (result instanceof Promise) { + void (async () => { + try { + const promise = persistCache?.(key, now, result) as unknown; if (promise instanceof Promise) promise.catch(noop); - }); - } else { + } catch { + // do nothing. + } + }); + } else { + try { const promise = persistCache?.(key, now, result) as unknown; if (promise instanceof Promise) promise.catch(noop); + } catch { + // do nothing. } - } catch { - // do nothing. } console.log(`Exiting ${context ? `method ${String(context.name)}` : 'function'}.`);