Async.waterfall 的本机回调函数
Native Callback function to Async.waterfall
我正在实现一个用Node编写的项目.js并从Mysql获取值。随着我在项目中的深入,我的嵌套回调像这样
hold.getEntry(function(data){
var ref = data.ref;
var id = data.id;
var message = data.mess;
var json = JSON.parse(message);
if(ref === null){
} else {
hold.checkPositionCandidate(ref, id, json, function(dataa){
console.log("checker " + dataa);
if(dataa == false){
} else {
//'/ here I get error callback is not a function
hold.getVoterCount(id, json, function(votercount){
if(votercount.count == 0){
} else {
checkVoter(ref, votercount.count, function(isallcorrect){
if(isallcorrect == false){
console.log('mali votes');
} else {
console.log('tama votes');
}
})
}
});
}
});
}
});
我得到"回调不是函数"。我已经研究并发现了"回调地狱",所以我找到了替代方案并使用异步.js
现在我的问题是如何将这段代码转换为 async.waterfall???有人可以帮助我吗????拜托了。
更新 1
我已经实现了 Peteb 的答案,但是当我执行它时,执行第一个函数。这是新代码。
var id = "";
var json = "";
async.waterfall([
function (callback) {
// hold.checkPositionCandidate
// if err return callback(err, null)
// if successful return callback(null, dataa)
hold.getEntry(function(data){
var ref = data.ref;
id = data.id;
var message = data.mess;
json = JSON.parse(message);
callback({'ref':ref, 'id':id, 'json':json});
console.log(data);
});
},
function (dataa, callback) {
// hold.getVoterCount
// if err return callback(err, null)
// if successful return callback(null, votercount)
if(dataa.ref === null){
callback(null);
}else{
hold.checkPositionCandidate(dataa.ref, dataa.id, dataa.json, function(dataaa){
callback(dataaa);
});
}
console.log('gfh');
},
function(anoData, callback) {
// checkVoter
// if err return callback(err, null)
// if successful return callback()
if(anoData === false){
} else {
hold.getVoterCount(id, json, function(votercount){
if(votercount == 0){
} else {
console.log('last function');
}
});
}
}
], function (err, results) {
// When finished execute this
});
async.waterfall
平展嵌套回调,并按照使用单个错误优先回调定义的顺序将结果从一个函数传递到下一个函数。因此,回调链中的每个步骤都将按照瀑布函数需要执行的顺序表示它们。
async.waterfall([
function (callback) {
// hold.checkPositionCandidate
// if err return callback(err, null)
// if successful return callback(null, dataa)
}),
function (dataa, callback) {
// hold.getVoterCount
// if err return callback(err, null)
// if successful return callback(null, votercount)
}),
function(votercount, callback) {
// checkVoter
// if err return callback(err, null)
// if successful return callback()
})
], function (err, results) {
// When finished execute this
});
编辑:更新的答案以解决更新的问题。
callback({'ref':ref, 'id':id, 'json':json}); // this is wrong
async.waterfall()
期望回调的第一个参数是Error
或null
。任何需要传递的值都位于 Error
参数之后。
// this is correct
return callback(null, { ref: ref, id: id, json: json });
// this is also correct
return callback(null, ref, id, json);
示例hold.getEntry()
hold.getEntry(function(data){
var ref = data.ref;
id = data.id;
var message = data.mess;
json = JSON.parse(message);
return callback(null, {ref: ref, id: id, json: json});
});
相关文章:
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定