Skip to content
Advertisement

how does reactor pattern work in Node.js?

enter image description here

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.

Advertisement

Answer

Everything 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. enter image description here

If there are no events in the event queue or the Event Demultiplexer has no pending requests, the program will complete.

note:callback queue that mentioned is event queue and call stack is execute handler.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement