[英]How to fix @typescript-eslint/no-misused-promises error
I have a onClick() method for a PrimaryButton class. I got @typescript-eslint/no-misused-promises error from eslint check.我有一个用于 PrimaryButton class 的 onClick() 方法。我从 eslint 检查中得到了 @typescript-eslint/no-misused-promises 错误。 Code as below:
代码如下:
onClick = { () =>
Utils.getName(a, b).then(
(name) => {
Utils.deleteThing(name, x, y);
})
}
How should I fix my code to avoid this error?我应该如何修复我的代码以避免此错误? Would appreciate your help.
感谢您的帮助。 Thanks!
谢谢!
Update: eslint config as follows更新:eslint配置如下
Error message is on the line of onClick():错误信息在 onClick() 行:
The error states that a void return was expected.该错误指出预期返回无效。 But instead, you are returning a promise.
但是,您返回的是 promise。
onClick = { () =>
Utils.getName(a, b).then(
(name) => {
Utils.deleteThing(name, x, y);
})
}
The way you write your arrow function, you are returning the below code (a promise) rather than running it and just returning nothing:你写箭头 function 的方式,你返回下面的代码(一个承诺)而不是运行它并且什么都不返回:
Utils.getName(a, b).then(
(name) => {
Utils.deleteThing(name, x, y);
})
However, if you write your code like this, void is returned and the code is actually ran:但是,如果您这样编写代码,则会返回 void 并实际运行代码:
onClick = { () => {
Utils.getName(a, b).then(
(name) => {
Utils.deleteThing(name, x, y);
})
}
}
The key is how an arrow function works.关键是箭头 function 是如何工作的。 If you leave out the
{
}
after the =>
, the return is implied to be the single statement after the =>
which is not what you wanted.如果您在
=>
之后省略了{
}
,则返回隐含为=>
之后的单个语句,这不是您想要的。 You didn't want to return the Utils.getName
function, you just wanted it to run.您不想返回
Utils.getName
function,您只是想让它运行。 See here for more info.有关更多信息,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.