Intellij Ideas 警告——“回报的承诺被忽略”,使用 aysnc/wait 命令

我在代码中使用 快递Node.js v7.3。在这里,我创建了一个 User Router,它将请求转发给我的 User Controller

我在 User Controller内部使用异步/等待来执行异步调用。问题是 IntelliJ 给了我一个警告

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

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

这是密码

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 有错?

编辑

多亏了@Stephen,我明白了异步函数返回一个承诺,但是如果 Intellij 识别出异步函数没有返回任何东西,并且没有显示这个警告,那不是更好吗? 因为当我在 login()函数之后链接一个 .then()时,它会提供一个 undefined对象到当时的结果中。这意味着如果我们不显式地从异步函数返回一些东西,那么未定义的东西就会被返回?

99433 次浏览

The userController.login() function returns a promise, but you're not doing anything with the result from the promise by utilizing its then() function.

For example:

userController.login(req, res).then(() => {
// Do something after login is successful.
});

or in the ES2017 syntax:

await userController.login(req, res);

If you don't actually want to do anything there, I guess you can just ignore the warning. The warning is mostly there because not using the then() function on a promise is usually a code smell.

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

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

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".

if you are really manic as me and the then() is not required but you need the warning to go away, a possible solution is:

functionWithAsync.error(console.error);

another way to get rid of the warning is defining an empty then():

userController.login(req, res); // <- Get the warning here

userController.login(req, res).then(); // <- No warning

I'm using try{} catch(e){} in NodeJs and found that simply adding Error() to the end of the function fixed the warning.

Full code:-

someArray.forEach(async (arrayValue) => {
try {
const prodData = await myAsyncFunc(arrayValue);
} catch(e) {
console.error(`Error: ${e}`);
}
}, Error());

You should use the "void" operator.

From MDN: void is for "evaluating expressions that produce a value into places where an expression that evaluates to undefined is desired."

router.post('/login', function (req, res, next) {
void userController.login(req, res); // Warning will not be shown now
});

If you simply want to shut this warning off for any of JetBrains products. Go to

Preferences > Inspections > JavaScript and TypeScript | Async code and promises | Result of method call returning a promise is ignored and turn the setting off.

functionWithAsync.catch();

In Angular it can be:

private async someMethod() {


await this.asyncMethod.catch();


}