我是async/await的新手,并且已经建立了一个基本的节点.js服务器来处理用户注册的表单数据。下面是我的代码
async.waterfall([async function(callback){ //Method 1
const hash = await bcrypt.hash(password, 10/*, () => { //breakpoint
console.log("Hash Generated Successfully");
}*/);
return hash;
}, function(hash, callback){ //Method 2
console.log(`The value of passed arg is: ${hash}`);
callback(null, 'success');
}], function(err, result){
if(err){
throw err
}
else {
console.log(result);
}
});
在Method 1
中,如果我不提供对bcrypt.hash()
的回调,代码可以正常工作,并且会打印哈希值。但是,如果我确实提供了回调,我会得到以下输出: The value of passed arg is: undefined
.所以,我这里有两个问题。1(为什么async.waterfall()
在向bcrypt.hash()
提供回调时中断?2( 除了回调之外,还有什么其他方法可以进行错误处理?
如果您计划将匿名函数作为参数包含,则必须将必要的参数传递给 bcrypt 回调函数。例如:
const hash = await bcrypt.hash(password, 10, (err, hash) => { // Added err, hash params.
console.log("Hash Generated Successfully");
});
return hash;