Home > other >  Does the fs.readFile() has its own event loop internally?
Does the fs.readFile() has its own event loop internally?

Time:11-01

when I used the fs.readFile() form the below code

fs.readFile('./Data/user.json',(err,data)=>{console.log('I/O')})

I noticed that the file got opened and data read form it and then it closed. If there is an internal event loop inside this method that is moves to the I/O phase then to the close Callback phase of the event loop and exit the program.

I have tried this code:

fs.readFile('./Data/user.json',(err,data)=\>{console.log('I/O')})

and added to its callback funtion:setTimeout and got the same result

fs.readFile('./Data/user.json',(err,data)=>{
    setTimeout(()=>console.log('timers-1'),0)
    console.log('I/O')
});

CodePudding user response:

Yes, it does have an event loop separate from the main event loop, you may need to disable this first before doing what you wanted too.

  • Related