Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js event loop understanding (with a diagram)

I've read this and this, watched this...

I've made a diagram of how I understand it:

enter image description here

  • Javascript callbacks (functions) can be present in the current queue, check queue, close callbacks queue, timers queue and I/O callbacks queue.
  • Js code gets executed only from the current queue one function (task/job) at a time.
  • Js code executed at the moment can add microtasks (jobs) to the current queue to be executed after itself and macrotasks (tasks) to the check queue. It can add tasks to other queues only inderectly by asking the API to do it.
  • Idle, prepare phase is used for some internal node js business (maybe like garbage collection).
  • Poll phase polls threads from the thread pool and fills the queues with appropriate callbacks.
  • Idle, prepare and poll phases don't have queues for js callbacks associated with them.
  • (four) Threads in the thread pool are all identical and have no specialization.
  • Event loop takes and executes tasks one by one from each queue until it's empty then moves on to the next queue.
  • Tasks in the queues don't have any jobs (microservices) associated with them. Jobs are created only during execution of a task or another job and are present only in the current task queue.

Is that understanding right or am I missing something?

MS Power Point .pptx file with the diagram can be found here.

like image 675
grabantot Avatar asked Aug 08 '17 10:08

grabantot


People also ask

What is node js event loop explain with diagram?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.

How do you explain an event loop?

The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, the Event Loop will take the first event from the queue and will push it to the Call Stack, which effectively runs it. Such an iteration is called a tick in the Event Loop.

How the node event loop works internally?

The Event Loop takes the timer with the shortest wait time and compares it with the Event Loop's current time. If the wait time has elapsed, then the timer's callback is queued to be called once the call stack is empty. Node. js has different types of timers: setTimeout() and setInterval() .

What is event loop example?

The event loop is a constantly running process that monitors both the callback queue and the call stack. In this example, the timeout is 0 second, so the message 'Execute immediately. ' should appear before the message 'Bye!' .


1 Answers

The diagram does seem quite complicated. I find a king analogy quite perfect in this context to have a grey level understanding about how event-loop works.

Imagine the code you want to run is a king and node is the army of servants.

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among his colleagues and they get to work.

Once a servant finishes a task, he lines up outside the kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king will give the servant more tasks on the way out.

Life is good, for the king's servants carry out all of his tasks in parallel, but only report with one result at a time, so the king can focus.

The king here is the main node process. This is how the nodejs is said to be single-threaded yet asynchronous.

like image 51
pntripathi9417 Avatar answered Sep 22 '22 02:09

pntripathi9417