从大文件流式传输并创建数组

时间:2016-09-23 14:21:54

标签: javascript node.js highland.js

我遇到了highland.js的问题。我需要从流数据中创建一系列函数,但无法使其工作。这是我的代码,但requests始终为空。

var requests = [];           
_(fs.createReadStream("small.txt", { encoding: 'utf8' }))
        .splitBy('-----BEGIN-----\n')
        .splitBy('\n-----END-----\n')
        .filter(chunk => chunk !== '')
        .each(function (x) {

            requests.push(function (next) {
                Helpers.Authenticate()
                    .then(function (response1) {
                        return Helpers.Retrieve();
                    })
                    .then(function (response2) {
                        return Helpers.Retrieve();
                    })
                    .then(function () {
                        next();
                    });
            });

        });
        console.log(requests)
        async.series(requests);

2 个答案:

答案 0 :(得分:1)

请阅读highland's doc。尝试将.done添加到您的信息流中,并console.log添加requests

_(fs.createReadStream("small.txt", { encoding: 'utf8' }))
    .splitBy('-----BEGIN-----\n')
    .splitBy('\n-----END-----\n')
    .filter(chunk => chunk !== '')
    .each(function (x) {

        requests.push(function (next) {
            Helpers.Authenticate()
                .then(function (response1) {
                    return Helpers.Retrieve();
                })
                .then(function (response2) {
                    return Helpers.Retrieve();
                })
                .then(function () {
                    next();
                });
        });

    }).done(function(){
      console.log(requests);
    });

答案 1 :(得分:0)

我只是使用流事件连接起来:

var stream = fs.createReadStream('small.txt', {encoding: "utf8"});

stream.on('data', (line) => {
    var lineStr = line.toString(); //Buffer to String
    /* You code here */
})

stream.on('close', (line) => {
    console.log(request);
})