最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

Share Improve this question asked Jan 30 at 8:00 Programmer54Programmer54 1699 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, bcryptpare 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.methodsparePasswords = async function (canditatePassword) {
  return bcryptpare(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.methodsparePasswords = async function (canditatePassword) {
  const isMatching = await bcryptpare(canditatePassword, this.password)
  debugger;
  return isMatching
}

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

发布评论

评论列表(0)

  1. 暂无评论