代码之家  ›  专栏  ›  技术社区  ›  Michael Brenndoerfer

如何调用异步清理函数?

  •  0
  • Michael Brenndoerfer  · 技术社区  · 2 年前

    useEffect(() => {
        return () => Voice.destroy().then(Voice.removeAllListeners);
      }, []);
    
    

    回调预期的效果 void Promise<void>

    1 回复  |  直到 2 年前
        1
  •  1
  •   Ron B.    2 年前

    你可以用大括号把身体包裹起来,因为现在它会返回 Promise (这是省略大括号时发生的情况,一行返回),这使清理函数返回一个 Promise<void> .对于大括号,它将只是函数的主体,而不是隐含的返回语句:

    useEffect(() => {
        return () => {
               Voice.destroy().then(Voice.removeAllListeners)
            };
      }, []);