liveBook · Manning

Macrotask queue

As explained in Secrets of the JavaScript Ninja, Second Edition

A queue that holds pending tasks for processing by an event loop.

When JavaScript is busy, a click does not immediately run its handler. The click event waits in the macrotask queue until the current task finishes. section 13.1.1

Two clicks while JavaScript runs

If mainline code runs for 15 ms and two buttons are clicked during that time, both click events wait in the queue. The event loop runs their handlers in turn after the current task completes. section 13.1.1

Figure 13.2. This timing diagram shows how events are added to the task queue as they occur. When a task is done executing, the event loop takes it off the queue, and continues by executing the next task.

Follow the timing diagram to see events enter the queue and tasks run in sequence. section 13.1.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Traces two click events through a timed queue example without detailing runtime internals.
Examples
Uses two button clicks that occur while 15 milliseconds of mainline code runs.
What you can do after
Understand why click handlers can wait until currently executing JavaScript finishes.
How it connects
Connects the task queue to the event loop, JavaScript execution, and event handlers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.