【问题标题】:Async - Nodejs doUntil inside waterfall异步 - 瀑布内的 Nodejs doUntil
【发布时间】:2014-10-09 10:09:57
【问题描述】:

我正在做一个项目,我需要 nodejs async 等到我从服务器获取数据

下面是我做的

异步响应:“done waterfall done doUntil false”而不是“doUntil false, done waterfall”

当我将服务器的响应更改为 OK 时,我收到错误“未定义的回调”

getData OK { 消息:'OK' }

TypeError: undefined is not a function 在 D:\node_modules\async\lib\async.js:729:17

var request = require('request-json')
    async   = require('async');

function startTest() {
    var self = this;
    self.on('fake', onFake);
    setInterval(function() {
        self.emit('fake', {fake: 'done'});
    }, 1000);
}

//doUntil
function onFake(data) {
    async.waterfall(
        [
            //get data and test against message === 'OK'
            function(next) {
                var _message = false;
                async.doUntil(
                    //iterator
                    function(done) {
                        getData(function(err, res) {
                            if(err) {
                                console.log('getData KO');
                                done(err, null);
                            }else{
                                console.log('getData OK', res);
                                done(null, res);
                            }
                        });
                    },
                    //test against
                    function(resp){
                        if( Boolean(resp.message === 'OK') ){
                            _message = Boolean(resp.message === 'OK');
                        }
                        return Boolean(resp.message === 'OK');
                    },
                    //done
                    next(null, 'done doUntil ' + _message)
                )
            },
            //done previous step
            function(message, next) {
                next(null, message);
            }
        ],
        function(err, res) {
            if(err) {
                console.log('done waterfall', err);
            }else{
                console.log('done waterfall', res);
            }
        }
    );
}

function getData(callback) {
    client = request.newClient('http://localhost/');
    client.get('cryptsy/stackoverflow/data.json', function(err, res, body) {
        if(!err) {
            return callback(null, body);
        }else{
            return callback(err, body);
        }
    });
}
startTest()

【问题讨论】:

    标签: node.js asynchronous nested async.js waterfall


    【解决方案1】:

    替换这个:

                        //test against
                        function(resp){
                            if( Boolean(resp.message === 'OK') ){
                                _message = Boolean(resp.message === 'OK');
                            }
                            return Boolean(resp.message === 'OK');
                        },
                        //done
                        next(null, 'done doUntil ' + _message)
                    )
    

    有了这个

                    //test against
                    function(resp){
                        if( Boolean(resp.message === 'OK') ){
                            _message = Boolean(resp.message === 'OK');
                        }
                        return Boolean(resp.message === 'OK');
                    },
                    //done
                    function(resp) {
                        next(null, resp);
                    }
                )
    

    【讨论】:

    • 简化版瀑布 fn1->result doUntil -> result = ok next fn2(result) console.log result done github.com/caolan/async#doUntil
    • 是的,我得到了这个 getData OK { message: 'OK' } 但是 console.log('done waterfall', err) 或 console.log('done waterfall', res) 从来没有调用过没有完成的瀑布?第二个功能
    • 如果我做 function(){ next(null, _message);} getData OK { message: 'OK' } 最后一个瀑布池完成了瀑布 true
    • 我真的认为你不需要异步。请求已经在等待响应。
    • 我需要结果才能进入下一个函数,我认为的唯一方法是 async doUntil 在瀑布内
    猜你喜欢
    • 2018-04-29
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2018-11-13
    • 2020-09-06
    • 2019-10-17
    • 2017-08-03
    相关资源
    最近更新 更多