Trishant Pahwa
Trishant Pahwa

Reputation: 2943

Wait for streams to completely update a variable

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.

Upvotes: 0

Views: 356

Answers (1)

Crawl
Crawl

Reputation: 26

Combined with How to use ES8 async/await with streams? and a bit "modernized" you'll want something like this.

async function getData() {
  const out = [];
  const chunks = { stream1: { start: '10', end: '20' }, stream2: { start: '20', end: '30' } };
  for (const key in chunks) {
    for await (const chunk of fs.createReadStream('file.txt', {
      start: parseInt(chunks[key].start),
      end: parseInt(chunks[key].end),
    })) {
      out.push(chunk.toString());
    }
  }
  return out;
}

Upvotes: 1

Related Questions