Keep the Event Loop Moving

In the last video, you followed a small laundry room program through the call stack, the waiting area, and the task queue, and you watched the event loop move each task onto the stack once the stack was empty. The video ended on a question: What happens when one task keeps the stack busy for a long time?

You’ve already seen the answer once, without the picture to explain it. In this lesson, you’ll use the picture to explain why Kofi’s reminder arrived late while Dev sorted the lost socks. Then you’ll find the code that holds up the event loop and split a long job into smaller pieces so the reminders get through.

Explain the Late Reminder

When you scheduled a reminder next to a long sort, the reminder came late, and you saw that a delay is a minimum. Here’s that script from Sort the Lost Socks again, saved as lost_socks.mjs:

Language: JavaScript Filename: lost_socks.mjs
setTimeout(() => {
  console.log("Dev starts sorting the lost socks");
  const socks = [];
  for (let i = 0; i < 1000000; i++) {
    socks.push(Math.random());
  }
  socks.sort((a, b) => a - b);
  console.log("Lost socks sorted");
}, 0);

setTimeout(() => {
  console.log("Kofi, collect your laundry");
}, 100);

Kofi’s reminder was due after 100 milliseconds, long before the sort finished, yet it printed last:

Language: Shell
$ node lost_socks.mjs
Dev starts sorting the lost socks
Lost socks sorted
Kofi, collect your laundry

Here’s the picture from the video again, so you can follow each step:

The call stack hands timers to the waiting area in Node, due timers join the task queue, and the event loop moves one onto an empty stack.
The Empty Stack Rule for Timer Callbacks

First, the script is on the call stack, the same stack you traced in Scope, Closures and the Call Stack. It calls setTimeout() twice, and Node.js keeps both callbacks in the waiting area. Then the script ends and leaves the stack empty. The sort’s timer is due first, so its callback goes to the task queue, and the event loop moves it onto the empty stack. The sort is now running.

At 100 milliseconds, Kofi’s timer is due, so his callback joins the task queue. Being due only puts it in line, though. The event loop only moves a task onto an empty stack, and the sort’s callback is still on it, so Kofi’s callback waits in the queue. Nothing interrupts the code that’s running. As in the video, this picture is a model. In Node.js, the loop checks which timers are due when it gets its turn, but the order comes out the same.

The sort itself uses a callback too: (a, b) => a - b. However, that one doesn’t wait. You hand it to .sort(), and .sort() calls it right away, many times, on the same stack. Not every callback runs later. Only a callback that you hand to something like setTimeout() waits its turn in the waiting area and the task queue.

When the timer callback that sorts the socks finally ends, the stack is empty again. The event loop gets its turn and moves Kofi’s callback from the task queue onto the stack. That’s run to completion at work: once a task starts, it runs to its end before the next one can start. A task that takes a second holds up every other task for a second.

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

Become a Member to join the conversation.