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');
                            }
                        })
                    }
                });
            }

        });
    }
});

我得到 "callback is not a function"。我已经研究并发现了 'callback hell' 所以我找到了一个替代方案并使用 Async.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 展平您的嵌套回调,并按照使用单个 error-first callback 定义的顺序将结果从一个函数传递到下一个函数。因此,回调链中的每个步骤都将按照它们需要执行的顺序代表您的瀑布函数。

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});
});