TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Lead says async func in useCallback is incorrect, use useMemo instead, WTF?

1 pointsby sakerbosover 1 year ago
Hello hackers! Normally I would discuss this with the team but it&#x27;s just me and our Tech Lead on the FE so I&#x27;m posting here for my own educational purposes.<p>I submitted a PR review containing an async function in a React useCallback earlier and our tech lead mentioned that:<p>&quot;You can&#x27;t async useCallback, it can cause unexpected behavior. To achieve a memoised async function instead use useMemo&quot;.<p>It was always my understanding that useCallback returned a memoised function and useMemo a memoised result or am I missing something here?<p>The asynchronicity should also be irrelevant or?<p>Any feedback would be appreciated, thanks!

2 comments

acemarkeover 1 year ago
You can memoize any function with `useCallback`, sync or async. All it&#x27;s doing is watching when the deps array changes, and saving whatever function reference was passed in at that time.<p>(Note that this does not mean that the function itself is memoized in terms of &quot;cache the result and check the arguments to see if we know this answer already&quot;.)
评论 #37565781 未加载
theGeatZhopaover 1 year ago
Just try it out. The programming language of your choice sure offers information and tools.