I am on Node-Mongo Environment. I am converting the String password to bcrypt. After this save it to the DB. But there is a problem that Bcrypt and after that functions not run in a sequential manner. For that, I used SetTimeout() function to run other function little late.
But as far as I know That this one is not a good Practice. Suppose if Anyhow Bcrypt Takes time and after that timer function call the otherone so may be collision Occur.
My Code is Like this:
var globalpasswordholder;
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
req.body.password = hash;
globalpasswordholder = req.body.password;
});
});
setTimeout(function(){
user.password = globalpasswordholder;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
user.save(function(err) {
req.logIn(user, function(err) {
done(err, user);
});
});
},1000);
If I not use setTimeout function then on console my globalpasswordholder is undefined.
Anyone have a solution for this. Thanks in advance.