Async.waterfall 的本机回调函数

Native Callback function to Async.waterfall

本文关键字:回调 函数 本机 waterfall Async      更新时间:2023-09-26

我正在实现一个用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()期望回调的第一个参数是Errornull。任何需要传递的值都位于 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});
});