Jyotman Singh
Jyotman Singh

Reputation: 11330

Intellij Idea warning - "Promise returned is ignored" with aysnc/await

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.

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

Promise returned from login() is ignored.

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

Here's the code -

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

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?

EDIT -

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?

Upvotes: 164

Views: 138876

Answers (10)

gildniy
gildniy

Reputation: 3903

The issue was discussed also on the JetBrains website, and if someone wants to get rid of the warning all they can do is ignore it from the IDE settings

Upvotes: 0

fingerprints
fingerprints

Reputation: 2960

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

// or use a logger
functionWithAsync.error(log.error);

Upvotes: 37

Aamir Kalimi
Aamir Kalimi

Reputation: 1931

In Intellij Ide

1. Hover over the yellow warning

enter image description here

2. Click on the More actions

enter image description here

3. Expand the options of first line In this case Add .then arrow sign

enter image description here

4. Then Click on the Edit inspection profile setting

enter image description here

5. Unchecked Result of method call returning a and then ok

Upvotes: 4

gzen
gzen

Reputation: 1714

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

Upvotes: 164

Tatyana Molchanova
Tatyana Molchanova

Reputation: 1583

functionWithAsync.catch();

In Angular it can be:

private async someMethod() {

 await this.asyncMethod.catch();

}

Upvotes: 0

Nate Bunney
Nate Bunney

Reputation: 2520

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.

Upvotes: 10

Joe Keene
Joe Keene

Reputation: 2351

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

Upvotes: 7

Alejandro Silva
Alejandro Silva

Reputation: 9108

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

Upvotes: 30

eekboom
eekboom

Reputation: 5802

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

Upvotes: 76

Robba
Robba

Reputation: 8324

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.

Upvotes: 85

Related Questions