I am reading Node.js Design Patterns. I am stuck in the understanding of the reactor pattern. I do not see any call stack here. I thought the call stack was one of the main parts of Node.js design. Can anyone please help me understand this diagram? Also, there is no callback queue.
I am reading Node.js Design Patterns. I am stuck in the understanding of the reactor pattern. I do not see any call stack here. I thought the call stack was one of the main parts of Node.js design. Can anyone please help me understand this diagram? Also, there is no callback queue.
Share Improve this question edited Jan 8, 2022 at 7:17 Yilmaz asked Jun 16, 2019 at 20:23 YilmazYilmaz 49.5k18 gold badges215 silver badges268 bronze badges 1- Yeah, I think a diagram like that would confuse anyone... if you were to just look at some example code rather than trying to interpret plicated diagrams that don't really clarify anything, I think whatever the "reactor pattern" is would be a lot clearer to you. My guess is, based on the name, that it means "event-based programming" which is very natural to write in Node.js. – Patrick Roberts Commented Jun 30, 2019 at 4:06
2 Answers
Reset to default 9Everything starts with the application, application makes requests and the event demultiplexer gathers those requests then forms queues, Event Queues. Event demultiplexer is run by libuv which is an asynchronous IO library that allows Node.js performs I/O.
In the diagram you see one event queue. actually there is not only 1 event queue, there are 7 basics queues. those queues have ascending priorities, the queue that highest priority checked first by the event loop.
Timers queue has the highest priority. setTimeout and setInterval functions get queued here. Once the events are done in this queue, or time is up, event loop passes those functions to call stack, in the diagram it is named execute handler.
Once one of the event queues are done, instead of jumping to next queue, event loop firstly will check 2 other queues which queues other micro tasks and process.nextTick functions. Then it will jump to next queue. this diagram will help u visualize the event loop.
If there are no events in the event queue or the Event Demultiplexer has no pending requests, the program will plete.
note:callback queue that mentioned is event queue and call stack is execute handler.
- The application generates a new I/O operation by submitting a request to the Event Demultiplexer.The application also specifies a handler, which will be invoked when the operation pletes. Submitting a new request to the Event Demultiplexer is a non-blocking call and it immediately returns control to the application.
- When a set of I/O operations pletes, the Event Demultiplexer pushes a set of corresponding events into the Event Queue.
- At this point, the Event Loop iterates over the items of the Event Queue.
- For each event, the associated handler is invoked.
- The handler, which is part of the application code, gives back control to the Event Loop when its execution pletes (5a). While the handler executes, it can request new asynchronous operations (5b), causing new items to be added to the Event Demultiplexer (1).
- When all the items in the Event Queue are processed, the Event Loop blocks again on the Event Demultiplexer, which then triggers another cycle when a new event is available.
Credit to packtpub.