Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  36] [ 5]  / answers: 1 / hits: 15350  / 7 Years ago, sat, october 7, 2017, 12:00:00

I'm looking to create a simple helper function which returns the hash for a given password using bcrypt but everytime i call the function, it resolves to Promises { <pending> } what am i doing wrong?



const saltPassword = async (password) => {
const newHash = await bcrypt.hash(password, saltRounds, (err, hash) => {
if (err) return err;
return hash;
});
return await newHash;
}


cheers


More From » async-await

 Answers
77

You should do something like this


const saltPassword = async (password) => {
const newHash = await bcrypt.hash(password, saltRounds, (err, hash) => {
if (err) return err;
return hash;
});
return newHash; // no need to await here
}

// Usage
const pwd = await saltPassword;

[#56293] Tuesday, October 3, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
saiget

Total Points: 64
Total Questions: 105
Total Answers: 105

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;