Only objects that require asynchronous processing are placed in the event loop. The standard event emitter in node will send the event immediately. Only code that uses things like process.nextTick
, setTimeout
, setInterval
or code that explicitly adds it to C ++ affects the event loop, such as node libraries.
For example, when you use the node fs
library for something like createReadStream
, it returns a stream, but opens the file in the background. When it is open, node adds events to the loop, and when the called function in the loop is called, it raises the "open" event in the stream object. Then, the node will load blocks from the file in the background and add the data
event trigger in the stream to the event loop.
If you want these events to come out after 5 seconds, you should use setTimeout
or put emit
calls after a busy cycle.
I would also like to be clear, you should never have such a busy cycle in node code. I canβt say if you are only doing this to check the event loop or if it is part of some real code. If you need more information, please extend the functionality you want to achieve.
source share