admin管理员组文章数量:1336576
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
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
Share Improve this question asked Oct 7, 2017 at 20:52 hloughreyhloughrey 9784 gold badges11 silver badges23 bronze badges 6-
saltPassword
is a promise, so you need to usethen
on it to get the value via a callback. That is how async code works. You cannot expect to get a function return value that only arrives later when the function has already returned. NB: the secondawait
is useless. – trincot Commented Oct 7, 2017 at 20:54 -
remove the
await
from thereturn
statement. – Traveling Tech Guy Commented Oct 7, 2017 at 20:55 -
async
functions always return promises. So you either need to callsaltPassword
from an async function and await it, or learn how to work with promises. – JLRishe Commented Oct 7, 2017 at 20:59 -
@trincot In this case, both
await
s are useless because OP isn't doing anything withnewHash
other than returning it. – JLRishe Commented Oct 7, 2017 at 21:02 -
you can shorten the whole function to
const saltPassword = async (password) => bcrypt.hash(password, saltRounds);
But this doesn't change anything about the async nature of this task and that you have to deal with the promise – Thomas Commented Oct 7, 2017 at 21:04
2 Answers
Reset to default 2You 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;
You need to return a Promise in order to use await
. Simply wrap the callback function and call reject if there is an error or resolve if it was successful.
const saltPasswordAsync = (password, rounds) =>
new Promise((resolve, reject) => {
bcrypt.hash(password, rounds, (err, hash) => {
if (err) reject(err);
else resolve(hash)
});
});
async function doStuff() {
try {
const hash = await saltPasswordAsync('bacon', 8);
console.log('The hash is ', hash);
} catch (err) {
console.error('There was an error ', err);
}
}
doStuff();
Now you can use await
to wait on the promise to resolve and use the value. To catch an error, wrap with a try/catch statement.
UPDATE
Thomas pointed out that you may not need to wrap the callback in a promise, since bcrypt returns a promise if you do not pass a callback function. You could replace the call to saltPasswordAsync
above with bycript.hash
like so:
const hash = await bcrypt.hash('bacon', 8);
console.log('The hash is ', hash);
本文标签: javascriptResolving Promise ltpendinggtStack Overflow
版权声明:本文标题:javascript - Resolving Promise <pending> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410837a2469752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论