node.js - bcrypt.compare - 'await' has no effect on the type of this expression - Stack Overflow

admin2025-04-18  3

I am using bcryptpare to compare the passwords:

UserSchema.methodsparePasswords = async function (canditatePassword) {
  const isMatching = await bcryptpare(canditatePassword, this.password)
  return isMatching
}

Based on the docs:

Asynchronously compares the given data against the given hash

But VSCode underlines the await keyword and shows a warning:

'await' has no effect on the type of this expression

Indeed, when I apply the quick fix and remove the await keyword, everything still works fine. Can anyone explain me, why is the await not needed here?

I am using bcrypt.compare to compare the passwords:

UserSchema.methods.comparePasswords = async function (canditatePassword) {
  const isMatching = await bcrypt.compare(canditatePassword, this.password)
  return isMatching
}

Based on the docs:

Asynchronously compares the given data against the given hash

But VSCode underlines the await keyword and shows a warning:

'await' has no effect on the type of this expression

Indeed, when I apply the quick fix and remove the await keyword, everything still works fine. Can anyone explain me, why is the await not needed here?

Share asked Jan 30 at 8:00 Programmer54Programmer54 1879 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

There are two things playing a role here:

  1. The VS Code warning: this warning is expected when the function returns a value that is not a promise, but in your case it does return a promise. However, bcrypt.compare has two signatures. When you provide a third argument to it (a callback), the function does not return a promise, and the warning would be warranted. I suppose that VS Code doesn't detect this nuance.

  2. Why it also works without await: In case you don't use await, then isMatching is a promise and not a boolean. That promise is returned. This means the promise returned by the async function is resolved with the promise isMatching (it is locked-in to it). That in turn means that the promise returned by the async function resolves to the boolean of interest, which is also the result when await is used.

    Note that if you choose to omit await, you can also omit async. That latter keyword is only useful for functions that need to execute await.

As other answers explain, await is not necessary indeed and won't affect the result of comparePasswords. Currently isMatching variable has no purpose, so it could be reduced to:

UserSchema.methods.comparePasswords = async function (canditatePassword) {
  return bcrypt.compare(canditatePassword, this.password)
}

And async could be discarded too because the function unconditionally returns a promise.

Debugging is a scenario that makes it desirable to have a variable and await that are optional in general:

UserSchema.methods.comparePasswords = async function (canditatePassword) {
  const isMatching = await bcrypt.compare(canditatePassword, this.password)
  debugger;
  return isMatching
}

This way isMatching boolean value can be easily observed at the breakpoint regardless of how comparePasswords is used.

转载请注明原文地址:http://www.anycun.com/QandA/1744932351a89666.html