可以通过使用箭头函数来解决此问题。以下是示例代码:
const bcrypt = require('bcrypt');
async function hashPassword(password) {
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
return hash;
}
以上代码可能会产生警告,因为await没有影响JavaScript函数的签名或返回类型。
通过使用箭头函数可以解决这个问题:
const bcrypt = require('bcrypt');
const hashPassword = async (password) => {
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
return hash;
};
通过使用箭头函数,警告将不再出现。