image.png

Node.js Event Loop and Async Behavior


1. What is the order of phases in the Node.js Event Loop?

Phases in order:

  1. Timers Phase – Executes setTimeout and setInterval.
  2. Pending Callbacks Phase – Executes I/O callbacks deferred to the next loop.
  3. Idle, Prepare Phase – Used internally.
  4. Poll Phase – Retrieves new I/O events, executes I/O callbacks.
  5. Check Phase – Executes setImmediate.
  6. Close Callbacks Phase – Executes close events like socket.on('close').

2. Does Poll phase come before Timeout phase?

Yes and No – It depends: