我一直在尝试按照下面显示的异步瀑布示例;
async.waterfall([
function(callback) {
callback(null, 'one', 'two');
},
function(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
这是我的代码版本;
var successfulRecords = 0 //successfulRecords holds the number of successfully processed rows
//Delete existing records for prepaid model before adding new records
async.waterfall([
function(callback){
redis.del("prepaid",function(err,data){
if (err){
logger.debug(err);
}
SCORES_CACHE = new Map(
rows.map(function(row){
redis.zadd("prepaid",row[1],row[0],function(err,response){
if (err){
logger.debug(err);
}
if (response==1){
successfulRecords=successfulRecords+1;
console.log(successfulRecords);
}
})
})
)
})
callback(null,successfulRecords);
},
function(arg1,callback){
console.log('hello, waiting for arg1: ',arg1);
}
], function(err,result){
});
代码应该像这样工作
但是,我不断得到这个结果;
hello, waiting for arg1: 0
我该如何解决这个问题?几个小时以来一直在努力。我没有正确放置回调吗?
提前致谢!
答案 0 :(得分:0)
在callback(null,successfulRecords);
完成之前调用此redis.del
。将它向上移动一行。
function(callback){
redis.del("prepaid",function(err,data){
if (err){
logger.debug(err);
}
SCORES_CACHE = new Map(
rows.map(function(row){
redis.zadd("prepaid",row[1],row[0],function(err,response){
if (err){
logger.debug(err);
}
if (response==1){
successfulRecords=successfulRecords+1;
console.log(successfulRecords);
}
})
})
)
callback(null,successfulRecords); //One line up
})
}