Skip to content

Chapter 28 of 33

The Event Loop

Reason about stacks, tasks, microtasks, rendering, starvation, and responsive scheduling.

42 minutes 10 quick checksBy Subha Prasad
Lesson 28 of 33Course navigation

Lesson content

Read, practise, then check your understanding

JavaScript jobs run to completion on a call stack. Hosts queue tasks for timers, events, and I/O; Promise reactions and queueMicrotask use the microtask queue.

Ordering

console.log("start");
setTimeout(() => console.log("task"), 0);
Promise.resolve().then(() => console.log("microtask"));
console.log("end");
// start, end, microtask, task

After a task, the host drains microtasks before another task and usually before rendering. Recursive microtasks can starve input/rendering; long synchronous work blocks everything. Split CPU work, yield appropriately, or move suitable computation to workers. Node has related phases and additional queues, so exact host ordering rules matter.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes call stack?
Which JavaScript term matches this description: The stack of currently executing JavaScript execution contexts.
Which statement best describes task?
Which JavaScript term matches this description: A queued unit such as a timer or dispatched event.
Which statement best describes microtask?
Which JavaScript term matches this description: High-priority follow-up work such as Promise reactions.
Which statement best describes run-to-completion?
Which JavaScript term matches this description: The rule that one JavaScript job finishes before another starts.
Which statement best describes render opportunity?
Which JavaScript term matches this description: A host chance to update presentation between event-loop work.

0 of 10 checks passed

Your progress is saved on this device.