【问题标题】:Native Callback function to Async.waterfall对 Async.waterfall 的本机回调函数
【发布时间】:2016-06-15 17:00:58
【问题描述】:

我正在实现一个用 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');
                            }
                        })
                    }
                });
            }

        });
    }
});

我得到“回调不是函数”。我研究并发现了“回调地狱”,所以我找到了替代方案并使用 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
});

【问题讨论】:

    标签: javascript node.js asynchronous callback bluebird


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。我会给你一个关于你的例子的更新。
    • 我在我的项目中应用了你的示例,但它只执行第一个函数
    • 您需要将我的 cmets 替换为您对这些函数的实际逻辑,并在执行完成后调用回调函数,以便调用下一个函数。
    • 是的,先生,我已经这样做了。顺便说一句,我发布了我改进的代码
    • @something 你没有正确实现你的回调。您需要使用错误优先回调。这就是异步构建回调的方式,就像 Node 中的大多数事情一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2018-07-05
    相关资源
    最近更新 更多