2

Say I have a readable stream, like so:

const strm = fs.createReadStream(file);
strm.on('data', function onData(d){});

my question is, does the onData callback fire in the next tick of the event loop? Or is it possible for the onData handler to be called twice in the same tick of the event loop?

1 Answers1

1

The data handlers can fire multiple times in the same tick of the event loop, see:

https://github.com/nodejs/help/issues/1222

Yes:

const stream = require('stream'); 
const readable = new stream.Readable({read(){}}); 
readable.on('data', () => console.log('data')); 
process.nextTick(() => console.log('next tick'));
readable.push('foo'); 
readable.push('foo'); 

Output:

data data next tick

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817