Promise 在函数参数中返回,其中预期返回 void

新手上路,请多包涵

我正在开发一个 Electron 应用程序,我想在我的 Main 中的匿名函数中使用异步等待,如下所示:

 process.on("uncaughtException", async (error: Error) => {
  await this.errorHandler(error);
});

但这会产生打字稿错误

Promise 在函数参数中返回,其中预期返回 void。

我正在使用 Typescript 3.9.7 和 Electron 9.2.0。

为什么它不允许我使用异步/等待?

原文由 mottosson 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 710
2 个回答

您可以在回调中使用异步 IIFE ,如下所示:

 process.on("uncaughtException", (error: Error) => {
  (async () => {
    await this.errorHandler(error);

    // ...
  })();
});

这确保回调的隐式返回仍然是 undefined ,而不是承诺。

原文由 Lionel Rowe 发布,翻译遵循 CC BY-SA 4.0 许可协议

修复了它,我从 这里 找到了下面的答案

 <div
  onClick={
    () => {
      void doSomethingAsync();
    }
  }
  onClick={
    () => {
      void (async () => {
        const x = await doSomethingAsync();
        doSomethingElse(X);
      })();
    }
  }
/>

原文由 Thanh Nhật 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题