TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

1 点作者 sakerbos超过 1 年前
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 条评论

acemarke超过 1 年前
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 未加载
theGeatZhopa超过 1 年前
Just try it out. The programming language of your choice sure offers information and tools.