Say we have the following if statement
.
.
if (!user.comparePassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
.
.
where comparePassword(password)
is a function to check if the user input password is identical to the one in the database.., and it's in another module being defined as follows:
userSchema.methods.comparePassword = function (password) {
let match = false;
bcrypt.compare(password, this.password)
.then(function (isMatch) {
if(isMatch){
match = true;
}
})
.catch(function (err) {
// handle error
});
return match;
};
The problem is in I/O.., where one could easily see the if
statement depends on a function comparePassword(password)
that could finish execution before the promises inside it bcrypt.compare()
finishes execution.., which means I get match
most of the time false
even if it should technically be true
. the compare function of bcrypt runs an expensive operation, so how do I get the result of comparison in time without blocking code-execution ?