简体   繁体   English

如何修复@typescript-eslint/no-misused-promises 错误

[英]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.

相关问题 Typescript 节点快速路由器和第二个参数。 Typescript-eslint/无误用承诺 - Typescript node express router and second argument. Typescript-eslint/no-misused-promises ESLint 发布 no-misused-promises,其中匿名函数用于等待多个异步调用 - ESLint issues no-misused-promises where anonymous function used to await multiple async calls 如何将 typescript-eslint 规则添加到 eslint - How to add typescript-eslint rules to eslint 如何修复“未找到规则‘@typescript-eslint/no-use-before-declare’的定义。eslint(@typescript-eslint/no-use-before-declare)” - How to fix 'Definition for rule '@typescript-eslint/no-use-before-declare' was not found.eslint(@typescript-eslint/no-use-before-declare)' 什么 JS 错误有助于防止 typescript 错误返回承诺的函数必须是异步的 @typescript-eslint/promise-function-async - What JS error help prevent with typescript error when it is Functions that return promises must be async @typescript-eslint/promise-function-async 如何配置@typescript-eslint 规则 - How to configure @typescript-eslint rules 如何禁用来自@typescript-eslint 的警告? - How to disable warnings from @typescript-eslint? 如何禁用@typescript-eslint/semi? - how disable @typescript-eslint/semi? 如何使用带有“this”的箭头 function 修复此 @typescript-eslint 警告? - How can I fix this @typescript-eslint warning using an arrow function with “this” inside? 如何修复“解析错误:“parserOptions.project”已为 @typescript-eslint/parser 设置。 在 gatsby-config 和 Gatsby-node.js 中 - How fix 'Parsing error: “parserOptions.project” has been set for @typescript-eslint/parser.' inside gatsby-config and Gatsby-node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM