問題描述
我正在開發一個 Electron 應用程序,我想在我的 Main 中的匿名函數中使用 async await,如下所示:
I'm working on an Electron application and I want to use async await in an anonymous function in my Main like this:
process.on("uncaughtException", async (error: Error) => {
await this.errorHandler(error);
});
但這會產生 Typescript 錯誤
But this yields the Typescript error
在返回 void 的函數參數中返回 Promise預計.
Promise returned in function argument where a void return was expected.
我正在使用 Typescript 3.9.7 和 Electron 9.2.0.
I'm using Typescript 3.9.7 and Electron 9.2.0.
為什么它不允許我使用 async/await?
Why doesn't it allow me to use async/await?
推薦答案
您可以使用異步 IIFE 在回調中,像這樣:
You can use an asynchronous IIFE inside the callback, like this:
process.on("uncaughtException", (error: Error) => {
(async () => {
await this.errorHandler(error);
// ...
})();
});
這確保回調的隱式返回保持undefined
,而不是一個promise.
This ensures that the implicit return of the callback remains undefined
, rather than being a promise.
這篇關于在預期返回 void 的函數參數中返回 Promise的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!