Skip to content

Commit

Permalink
fix: withCache errors cached forever (#598)
Browse files Browse the repository at this point in the history
* fix withCache errors cached forever

* chore: changeset

---------

Co-authored-by: moxey.eth <[email protected]>
  • Loading branch information
iuriiiurevich and jxom authored May 29, 2023
1 parent 57bf239 commit baea299
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-islands-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed an issue in `withCache` where the promise cache would not clear upon rejection.
20 changes: 11 additions & 9 deletions src/utils/promise/withCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ export async function withCache<TData>(
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()
}
}

1 comment on commit baea299

@vercel
Copy link

@vercel vercel bot commented on baea299 May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.