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

streaming from large files and creating an array

我在使用 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);

我只想使用流事件来连接:

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

只需阅读 highland's 文档。尝试将 .done 添加到您的流中,并在 requests.

中添加 console.log
_(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);
    });