Home > Blockchain >  Why are chunks out of order when using stream.Readable.from?
Why are chunks out of order when using stream.Readable.from?

Time:10-26

I would assume when reading the chunks, it would start with 'applesauce', 'blueberry', etc. but instead the iterable that was used to initially create the stream is always last rather than first, despite the pushes being in the correct order. Can someone explain why the order is the way it is?

import stream from 'node:stream';

const a = stream.Readable.from(['applesauce', 'blueberry', 'muffins']);

a.push('one');
a.push('two');
a.push('three');
a.push('333');
a.push('555');
a.push('777');

for await (const chunk of a) {
  console.log(chunk);
}

Prints:

one
two
three
333
555
777
applesauce
blueberry
muffins

CodePudding user response:

How I'm reading the implementation of stream.Readable.from(), a readable stream will start reading from the iterable — and pushing its data onto its internal read queue — only once it's being read from itself.

So any data pushed onto the queue before reading the stream will precede the items from the iterable.

  • Related