See where a timer’s callback waits and when it runs: Node runs one thing at a time on the call stack, a callback whose time is up waits in the task queue, and the event loop moves it onto the stack once the stack is empty. Learn why a 0 ms callback runs after the file, why a busy stack makes a callback late, and why a loop that waits for a callback never ends.
TypeScript
Fundamentals
The event loop, in pictures. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
In the last lesson, a reminder set for zero milliseconds still printed
after the line written below it. There was almost nothing to count, and
yet it waited. That is not a quirk of zero: it is how Node
treats every timer's callback, and once you can see how, you can say
when each of them will run.
Picture a small café kitchen with one cook. The cook makes one dish at
a time and finishes it before starting the next. When the oven timer
rings, nobody pulls the cook away from the pan: the ticket for that
dish is clipped to a rail, behind the tickets already there, and the
cook takes the next ticket from the rail only with free hands. Node
runs your program the same way. In this lesson you meet its three
parts, and by the end you can follow what Node does with a program
that waits, and say when each of its callbacks runs.
One stack, one thing at a time
Lesson 5.8 showed the call stack: a pile of frames, one for every
call under way, the newest on top. Only the top frame runs; every
frame under it waits. Your file itself is the bottom frame, global,
so the stack is empty only once the whole file has run. Node has one
call stack for your program, and it is the cook's one pair of hands:
one thing runs at a time, and nothing runs beside it.
A timer is not on the stack. setTimeout gets a frame like any call,
hands the callback and the time over to Node, and returns. Node counts
the time outside your code, the way an oven counts on its own; that
was the Timer lane in the last lesson's first figure. So while a timer
counts, the stack is free for the lines after it.
Where a callback waits: the task queue
When the time is up, the callback cannot simply run: your code may
still be running on the stack at that moment, and Node never runs two
things at once. So the callback waits its turn.
A queue is a group of things waiting their turn, like people at a
shop's checkout: whoever joined first is served first, and whoever
joins later goes to the back. Programmers call this first in, first
out. Node keeps a queue for callbacks that are ready to run, the
task queue: it is the cook's rail of tickets. When a timer is
done, Node puts its callback at the back of the task queue. Nothing in
the queue runs by itself: it waits there until something takes it
out.
The event loop
That something is the event loop, the part of Node that moves
callbacks from the queue to the stack. It does one job, again and
again, which is why it is called a loop:
When the call stack is empty, it takes the first callback in the
task queue.
It pushes that callback onto the stack, where it runs like any
call, to its end.
When the stack is empty again, it goes back to the queue.
When the stack and the queue are both empty and no timer is still
counting, nothing is left to do, and Node ends the program. That is
the last lesson's rule, "Node waits for timers", seen from inside.
Here is a program of five lines: line 1 prints List opened, lines 2
to 4 set a timer of 0 ms (milliseconds, thousandths of a second), and
line 5 prints Reminder set. Press Next step to walk through it,
or let it play:
The file runs as global, the bottom frame of the stack. Line 1
prints.
The first line is out.
Line 2 hands the callback to a timer, and the call returns at
once.
Node counts 0 ms as 1 ms, almost nothing, so it is done almost at once.
The timer is done, so its callback joins the task queue.
Line 5 runs. The stack is busy, so the callback has to wait.
Line 5's text is out, before the reminder.
The file is done: global comes off, and the stack is empty.
The event loop sees the empty stack and moves the callback onto it.
The callback prints. Nothing is left to wait for, so Node ends.
The event loop moves the callback once the stack is empty
All 10 steps shown.Step 1 of 10: Call stack: line 1Step 2 of 10: Terminal: List openedStep 3 of 10: Call stack: setTimeoutStep 4 of 10: Timer: 0 msStep 5 of 10: Task queue: callback waitsStep 6 of 10: Call stack: line 5Step 7 of 10: Terminal: Reminder setStep 8 of 10: Call stack: emptyStep 9 of 10: Call stack: callbackStep 10 of 10: Terminal: Reminder: call Mom
All 10 steps shown.
Watch the Task queue lane. Even a timer that is done almost at
once cannot cut in: its callback stays in the queue while line 5 runs
and while global comes off the stack. Then watch the Call stack
lane: once it shows empty, the event loop moves the callback onto it.
This is the last lesson's zero.ts with a line on top and another last
line. Press Run to see the order:
It prints List opened, then Reminder set, and last Reminder: call Mom, as the figure showed.
A busy stack keeps the queue waiting
The event loop moves a callback only when the stack is empty. So when
one line keeps the stack busy for a long time, every callback in the
queue waits for it, however short its timer was.
The next two programs run in your terminal, as in the last lesson:
busy.ts keeps the computer busy, and wait.ts, further on, never
ends until you stop it. In ~/projects/taskkit, make busy.ts. It sets
a reminder for 100 milliseconds from now, then counts to two billion
with a while loop (lesson 4.2), which keeps the computer busy for
about a second, and prints when it is done:
The stack is busy with the loop, then with line 8, so the callback
keeps waiting.
Done counting.
Line 8 prints it. It runs on the stack too, so the callback still
waits.
The timer is done at 100 ms, but the callback waits for the loop
The timer was done after a tenth of a second, and its callback joined
the queue. But the while loop was on the stack, so the event loop
could not move the callback, and it waited for the rest of the second.
The Call stack lane draws the loop and the callback; line 8, which
prints Done counting, is the short stretch between them, and the
stack is not empty until it has run. The time you give setTimeout is
the shortest wait, never an exact one: the callback runs once its time
is up and the stack is empty.
Predict the order
Three timers of zero milliseconds, and the first callback sets the
third timer itself. Read the program, then decide which line it
prints last.
Sometimes the lines after a timer need what its callback does. A loop
that waits until the callback has run looks like the answer. It makes
sure the callback never runs.
Common mistake
A loop that waits for a callback
Make wait.ts. The callback sets reminded to true after a second,
and the while loop asks !reminded again and again until reminded
is true:
wait.tsts
let reminded = false;setTimeout(() => { reminded = true;}, 1000);console.log("Waiting for the reminder");while (!reminded) {}console.log("Reminder: call Mom");
The loop's body is empty, {}: all it does is ask its question again.
Run npm run check, with npm (Node package manager, the tool that runs
the project's scripts): it passes. Then node wait.ts prints one line
and then nothing, ever. The prompt does not come back:
Outputtext
Waiting for the reminder
The timer is done after a second, and the callback joins the queue.
But the while loop is on the stack and never ends, so the stack is
never empty, the event loop never moves the callback, and reminded
stays false for good. Stop the program the way lesson 4.2 showed:
⌃ Control+C
Ctrl+C
Ctrl+C
Waiting for the reminder
The fix
Do not wait for the callback: put what needs it inside the callback,
as in the last lesson.
wait.tsts
setTimeout(() => { console.log("Reminder: call Mom");}, 1000);console.log("Waiting for the reminder");
It prints Waiting for the reminder, then Reminder: call Mom a
second later, and ends.
You can recognise it by a program that prints its first lines, then
stops printing and never gives the prompt back: some loop is waiting
for a change that only a callback can make.
A while loop waits for a callback to run. What happens?
Check yourself
When does the event loop move a callback onto the stack?
Check yourself
When does the callback of setTimeout(callback, 100) run?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
The event loop, in pictures
What things are
Word
What it means
Call stack
The pile of calls under way; only the top one runs
Queue
Things waiting their turn: first in, first out
Task queue
Callbacks whose time is up, waiting their turn
Event loop
Moves the first callback in the queue to the empty stack
What happens to a callback
Step
Where it is
The timer starts
With the timer, counting outside your code
The time is up
At the back of the task queue
The stack is empty, and it is first
On the stack, running to its end
Rules
Rule
What it means
One thing at a time
A timer's callback waits for the running code
First in, first out
Ready callbacks run in the order they joined
The time is the shortest wait
A busy stack makes a callback late
Never wait in a loop
A loop waiting for a callback never ends
Node ends when nothing is left
Empty stack, empty queue, no timer
Where to go next
You can now follow a program that waits through Node's three parts:
the call stack, where one thing runs at a time; the task queue, where
ready callbacks wait their turn; and the event loop, which moves the
first of them onto the stack each time it is empty.
Next is Promise<T>. A callback is one way to say what happens
when a wait ends; a promise is another, a value that stands for a
result still to come, and it is what this module's TaskKit step is
built on.
Before you move on, change busy.ts's 100 to 0, predict which
line comes first, and check with node busy.ts. Then delete busy.ts,
queue.ts and wait.ts from the taskkit folder: they are not part of
TaskKit, and wait.ts never ends.