I am trying to obtain some data from multiple streams, and appending them to a particular variable. The code is as follows.
const fs = require('fs');
data = [];
chunks = {"stream1": { "start": "10", "end": "20"}, "stream2": { "start": "20", "end": "30"}};
for(let key in chunks) {
var stream = fs.createReadStream('file.txt', {start: parseInt(chunks[key].start), end: parseInt(chunks[key].end)});
stream.on('data', function(chunk) {
data.push(chunk.toString());
}
}
console.log(data);
But, what data outputs is an empty array []
. Someone please help me, to get the data in the data
variable and then display it, not before that.
I thought of creating an event that is emitted when the length of the array reaches a particular number, but it just seemed weird.