diff --git a/.changeset/brave-islands-agree.md b/.changeset/brave-islands-agree.md new file mode 100644 index 0000000000..b988684f27 --- /dev/null +++ b/.changeset/brave-islands-agree.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Fixed an issue in `withCache` where the promise cache would not clear upon rejection. diff --git a/src/utils/promise/withCache.ts b/src/utils/promise/withCache.ts index 4b131fb996..0035e10ca8 100644 --- a/src/utils/promise/withCache.ts +++ b/src/utils/promise/withCache.ts @@ -59,15 +59,17 @@ export async function withCache( cache.promise.set(promise) } - const data = await promise + try { + const data = await promise - // Clear the promise cache so that subsequent invocations will - // invoke the promise again. - cache.promise.clear() + // Store the response in the cache so that subsequent invocations + // will return the same response. + cache.response.set({ created: new Date(), data }) - // Store the response in the cache so that subsequent invocations - // will return the same response. - cache.response.set({ created: new Date(), data }) - - return data + return data + } finally { + // Clear the promise cache so that subsequent invocations will + // invoke the promise again. + cache.promise.clear() + } }