返回的承诺被忽略

返回的承诺被忽略

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

问题描述

我在 Node.js v7.3的代码中使用 Express.js 。在这里,我创建了一个用户路由器,它将请求转发给我的用户控制器

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.

我在用户控制器中使用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()方法返回任何内容。

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});
    }
};

如果我使用本机承诺编写相同的登录方法,那么我不会收到此警告。我在这里理解错误或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,我知道async函数会返回一个promise,但是如果Intellij发现没有从async函数返回任何内容并且没有显示该警告因为我链接<$ c $时不会更好c> .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?

推荐答案

声明为async的函数返回一个Promise根据定义。
请参阅

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-enter,right并更改检查级别以使警告消失。
检查在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:28