Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Watch the Event Loop Work

In this lesson, you followed a small laundry-room program through a picture of what Node does behind the scenes, and saw why a timer that’s due waits while JavaScript is busy.

Key Points

  • The picture has three places. The call stack holds the functions running right now, each on top of the one that called it. When you run a file, the script itself is the first thing on the stack.
  • When your code calls setTimeout(), Node starts a timer and keeps the callback in the waiting area. The waiting area is Node’s, outside your code, so the script carries on at once.
  • When a timer is due, its callback is ready to run. The picture shows that by moving it to the task queue. A task is a ready callback, waiting its turn. Tasks line up in the order their timers came due, not the order the script scheduled them.
  • The event loop takes the first task from the queue and puts it on the stack. It runs on the same thread as your code, so it only gets a turn when the call stack is empty. Until then, nothing interrupts the code that’s running.
  • This is the picture behind now and later. The script on the stack is now, and every callback still waiting, in the waiting area or the task queue, is later. Later starts only when the script finishes and leaves the stack empty.
  • Run to completion means that once code starts, the script or a task, it runs to its end before the next task starts.
  • That’s why a delay is a minimum, not a promise. Wren’s washer was due at one tenth of a second, but her line printed at three tenths, once the stack was empty.
  • When the queue and the waiting area are both empty, Node ends the program.
  • The picture is a model. In Node, the loop checks which timers are due when it gets its turn. The picture moves them into the queue early, to show they’re ready. For timers, the order is the same.

The rule: a timer callback runs only when the event loop moves it from the task queue onto an empty call stack.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Every timer callback in this course so far has run later. When you built the nag, you met a caveat: a timer that’s due waits while JavaScript is busy. In this video, you’ll see where it waits, and what decides when it runs.

00:16 Here’s a small program for the laundry room. The seconds() helper stamps each line with the time since the start. The checkMachines() function is a busy loop.

00:25 It holds JavaScript for 300 milliseconds. It’s a toy, but real work can take that long. Then the program schedules two reminders. Kofi’s dryer is done after 200 milliseconds.

00:38 Wren’s washer is done after 100. Last, the program checks every machine and logs that it’s finished. To follow it, you need a picture with three places, and a clock.

00:52 The first place is the call stack. You know it from stack traces: the functions running right now, each on top of the one that called it. When you run the file, the script itself is the first thing on the stack.

01:05 The script calls setTimeout() for the dryer. Node starts a timer and keeps the callback in the second place, the waiting area. The waiting area is Node’s, outside your code, so the script carries on at once.

01:19 The washer’s timer joins it.

01:23 Next, the script calls checkMachines(), which goes on top of the stack. Its busy loop spins.

01:31 At one tenth of a second, the washer’s timer is due, so its callback is ready to run. The picture shows that by moving it to the third place, the task queue.

01:42 A task is a ready callback, waiting its turn. At two tenths, the dryer’s callback joins the queue, behind Wren’s. Wren’s task is first because her timer came due first, even though the script scheduled it second.

01:58 Both tasks are ready, but neither can run yet. The event loop takes the first task from the queue and puts it on the stack. It runs on the same thread as your code, so it only gets a turn when the call stack is empty.

02:12 Until then, nothing interrupts the code that’s running.

02:16 At three tenths of a second, the busy loop ends, and checkMachines() returns. So, which line prints next? Your turn: pause the video and predict the line.

02:33 Checked every machine. The script is still on the stack, so its last line runs first. That’s your now and later. The script is now, and callbacks waiting, in the waiting area or queue, are later.

02:48 Later starts only when the script finishes and leaves the stack empty.

02:54 Now the event loop moves Wren’s task onto the stack, and her line prints at three tenths of a second. Her washer was due at one tenth. A delay is a minimum, not a promise.

03:06 The task had to wait for an empty stack.

03:10 Wren’s callback runs to its end and leaves the stack. The loop moves Kofi’s task across, and his line prints. With the queue and the waiting area empty, Node ends the program.

03:24 So here’s the rule: a timer callback runs only when the event loop moves it from the task queue onto an empty call stack. And run to completion means that once code starts, the script or a task, it runs to its end before the next task starts.

03:41 This picture is a model. In Node, the loop checks which timers are due when it gets its turn. The picture moves them early, to show they’re ready. For timers, the order is the same.

03:52 That’s all you need about it for now.

03:55 Wren’s reminder was two tenths of a second late. Real work can make it seconds. So how do you keep the stack free? Next, in Keep the Event Loop Moving, you’ll find code that holds up the loop, and split a long job so reminders arrive on time.

Become a Member to join the conversation.