如何在async.waterfall中调用cb(),同时调用.then
请看下面的代码
async.waterfall([
function check_network(cb) {
cb("ERROR-25", "foo") //<<----- This works
},
function process_pay(cb){
somePromise().then((status)=>{
if(status){
cb(null, status) //<<----ERROR---- can't call cb() it looses the scope
}
cb("ERROR-26") //<<--ERROR------ Same issse as above
})
},
function print(cb){
//some code
} ])发布于 2018-08-21 11:17:52
在瀑布函数中:结果值作为参数传递给下一个任务。
此外,回调中的第一个参数也保留为错误。所以当这行被执行时
cb("ERROR-25")这意味着抛出了一个错误。所以下一个函数不会被调用。
现在要问的问题是,‘不能调用cb(),它会松开范围’‘。万一check_network cb被调用如下所示
cb(null, "value1");process_pay的相应定义如下:
function process_pay(argument1, cb){
somePromise().then((status)=>{
if(status){
cb(null, status)
}
cb("ERROR-26")
})
}这里,argument1将是'value1'。
最后的代码应该类似于
async.waterfall([
function check_network(cb) {
// if error
cb("ERROR-25") // Handled at the end
// else
cb(null, "value1") // Will go to next funtion of waterfall
},
function process_pay(arg1, cb){
somePromise().then((status)=>{
if(status){
cb(null, status) // cb will work here
}
cb("ERROR-26") // Error Handled at the end
})
},
function print(arg1, cb){
//some code
}
], function(error, result){
// Handle Error here
})有关异步瀑布的更多信息,请访问此链接
https://stackoverflow.com/questions/51946177
复制相似问题