Skip to content
Optimal Lab

Node.js · interactive

The event loop, phase by phase

Synchronous code finishes first. Queued callbacks then run in a fixed order. Turn some off and run the loop again.

Call stack

empty

nextTick queue

process.nextTick

Promise queue

Promise.then

Timers

setTimeout(0)

I/O (poll)

fs.readFile callback

Check

setImmediate

Run one turn and watch callbacks leave their queues for the call stack.

Queue callbacks, then run one turn.

  1. 1

    The call stack runs to the end. Nothing queued can start yet.

    console.log("start")
    console.log("end")

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

This is the order when the timer is already due and the file callback is already ready. If the timer is still waiting, poll can run first.