Node.js event loop understanding (with a diagram)

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)