Async - Nodejs在瀑布内部运行

时间:2014-08-15 16:33:10

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

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

下面是我做的

async的响应:“完成瀑布完成doUntil false”而不是“doUntil false,done waterfall”

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

getData OK {message:'OK'}

TypeError:undefined不是函数     在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()

1 个答案:

答案 0 :(得分: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);
                }
            )