Intellij Idea警告-“返回的承诺被忽略"与aysnc/await [英] Intellij Idea warning - "Promise returned is ignored" with aysnc/await

查看:89
本文介绍了Intellij Idea警告-“返回的承诺被忽略"与aysnc/await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js v7.3的代码中使用了 Express.js .在此,我创建了一个 User Router ,它将请求转发到我的 User Controller .

I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller.

我正在 User Controller 中使用async/await进行异步调用.问题是IntelliJ给我一个警告,说

I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me a warning saying that

login()返回的承诺将被忽略.

Promise returned from login() is ignored.

问题是,我什至没有从 login()方法返回任何内容.

The thing is I'm not even returning anything from the login() method.

这是代码-

UserRouter.js

router.post('/login', function (req, res, next) {
    userController.login(req, res); // I get the warning here
});

UserController.js

exports.login = async function (req, res) {
    try {
        const verifiedUser = await someFunction(req.body.access_code);
        let user = await User.findOrCreateUser(verifiedUser);
        res.status(200).send(user);
    }
    catch (err) {
        res.status(400).send({success: false, error: err});
    }
};

如果仅使用本机Promise编写相同的登录方法,则不会收到此警告.我在这里理解错了吗?还是IntelliJ错了?

If I write the same login method using native promises only then I don't get this warning. Am I understanding something wrong here or is IntelliJ at fault?

编辑-

感谢@Stephen,我知道异步函数返回了promise,但是如果Intellij识别出异步函数未返回任何内容并且不显示该警告,那会更好,因为当我链接一个 .then() login()函数之后,它为当时的结果提供了一个 undefined 对象.这意味着如果我们不从异步函数中显式返回某些内容,则返回undefined吗?

Thanks to @Stephen, I understand that an async function returns a promise but wouldn't it be better if Intellij identifies that nothing is being returned from the async function and doesn't show that warning because when I chain a .then() after the login() function, it provides an undefined object into the then result. It means if we don't return something from the async function explicitly then undefined is returned?

推荐答案

问题是我什至没有从login()方法返回任何东西.

The thing is I'm not even returning anything from the login() method.

声明为异步"的函数根据定义返回Promise.例如,请参见 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function

A function declared "async" returns a Promise by definition. See for example https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

但是,IDEA警告仅是检查.您可以按警告上的"alt输入,向右"并更改检查级别以使警告消失.该检查位于"JavaScript->可能的错误"类别中,并且被命名为忽略返回承诺的方法调用的结果".

However the IDEA warning is only an inspection. You can press "alt-enter, right" on the warning and change the inspection level to make the warning go away. The inspection is in the "JavaScript -> Probable bugs" category and is named "Result of method call returning a promise is ignored".

这篇关于Intellij Idea警告-“返回的承诺被忽略"与aysnc/await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆