async.waterfall() 在将回调传递给 bcrypt.hash() 时中断
async.waterfall() breaks on passing callback to bcrypt.hash()
我是 async/await 的新手,已经设置了一个基本的 node.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;
我是 async/await 的新手,已经设置了一个基本的 node.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;