Home > Back-end >  When does this event emitter start?
When does this event emitter start?

Time:09-17

I wrote some code that is working just the way I want it in Node v14.15.1, but I don't entirely understand how it works. Here it is:

    const rows = await new Promise(resolve => {
        const result = [];
        const file = fs.createReadStream('src/tests/readability/input.csv');
        const reader = file.pipe(csv());
        reader.on('data', row => {
            result.push(row);
        });
        reader.on('end', () => {
            resolve(result);
        });
    });

My question is, when does the event emitter start firing events and calling the callback functions? Because at no point do I ever call any kind of start() function. I assign callback functions using reader.on(), and then the CSV file just gets read. Like how come it doesn't start reading the file before I have a chance to assign the callbacks, therefor making me miss the start of the events or even all the events?

CodePudding user response:

The stream will start to emit data when you attach the data handler

https://nodejs.org/api/stream.html#stream_event_data

Attaching a 'data' event listener to a stream that has not been explicitly paused will switch the stream into flowing mode. Data will then be passed as soon as it is available.

https://areknawo.com/node-js-file-streams-explained/

Flowing mode

"Flowing mode" is definitely a bit more complex in its nature. Here, the .read() method is called automatically, leaving you only with consuming given data within the "data" event, emitted right after .read() call, with a fresh data chunk.

// ...
readable.on("data", dataChunk => {
    // code
});

Furthermore, "flowing mode" has a safeguard built-in, that prevents the data from being automatically read, if a proper handler isn't available. So, only when you add your "data" event handler, data will start flowing. As mentioned earlier, this also makes a switch from "paused" to "flowing" mode take place. You still need to be cautious though! Calling .resume() method without "data" event handler, or removing the handler, won't stop the reading process and will result in data loss!

CodePudding user response:

In Order to understand when the event emitter is fired, first you need to understand how event loop works in Javascript which explained in the video.

Basically when the code run, when it encounters

const file = fs.createReadStream('src/tests/readability/input.csv');

It is passed on the the Web API, even if the data is available at that time it will be only enter the event loop as a new entry so it need to wait till all the entry before it to get executed. When the entry is executed it will already have the callback function attached to the respective entry.

reader.on('data', row => {result.push(row);});reader.on('end', ()=> {resolve(result);});

  • Related