我遇到了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);
答案 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);
})