mot*_*son 5 javascript typescript electron
我正在开发一个 Electron 应用程序,我想在我的 Main 的匿名函数中使用 async await,如下所示:
process.on("uncaughtException", async (error: Error) => {
await this.errorHandler(error);
});
Run Code Online (Sandbox Code Playgroud)
但这会产生 Typescript 错误
Promise 在函数参数中返回,预期返回 void。
我正在使用 Typescript 3.9.7 和 Electron 9.2.0。
为什么它不允许我使用 async/await?
Tha*_*hật 19
修复了它,我从这里找到了下面的答案
<div
onClick={
() => {
void doSomethingAsync();
}
}
onClick={
() => {
void (async () => {
const x = await doSomethingAsync();
doSomethingElse(X);
})();
}
}
/>
Run Code Online (Sandbox Code Playgroud)
Lio*_*owe 13
您可以在回调中使用异步IIFE,如下所示:
process.on("uncaughtException", (error: Error) => {
(async () => {
await this.errorHandler(error);
// ...
})();
});
Run Code Online (Sandbox Code Playgroud)
这确保了回调的隐式返回仍然是undefined,而不是承诺。
Far*_*zan 10
您可以借助checksVoidReturn option关闭此检查(不是整个规则) :
.eslinter.js
rules: {
{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksVoidReturn": false
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
但我强烈建议不要这样做,并使用.then和重写代码.catch,以避免遇到此 linter 错误。
您还可以在该特定事件上方的注释中禁用此规则// eslint-disable-next-line @typescript-eslint/no-misused-promises,并添加几行注释来描述您认为可以忽略该错误的原因。
| 归档时间: |
|
| 查看次数: |
6342 次 |
| 最近记录: |