liveBook · Manning

Event loop

As explained in Build a Frontend Web Framework (From Scratch)

The JavaScript runtime mechanism that coordinates execution of synchronous code and queued asynchronous tasks.

The event loop coordinates synchronous JavaScript with asynchronous work. When the execution stack is empty, it moves callbacks from the task and microtask queues onto that stack. section 13.3.2

A simplified event-loop cycle proceeds as follows:

  • Execute every pending microtask in order, including microtasks added while earlier microtasks run. Promise callbacks are common microtasks. section 13.3.3 section 13.3.2
  • Execute the oldest eligible task, such as an event-listener, setTimeout(), or setInterval() callback. section 13.3.3 section 13.3.2
  • Allow the browser to render pending DOM changes, then begin another cycle while tasks remain. section 13.3.3 section 13.5

Scheduling work after updates

A scheduler can queue its update-processing function as a microtask. A nextTick() function can then return a Promise that resolves after pending scheduler jobs finish, allowing code after await nextTick() to run after the component has re-rendered. section 14.1.2

Figure 13.5 Execute all the microtasks in the microtask queue.
figure

Use the cycle diagram to trace why all pending microtasks run before the next task. section 13.3.3

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Explains the execution stack, two queues, four cycle steps, and microtask priority without pursuing full specification detail.
Examples
Uses event listeners, timers, promise callbacks, lifecycle hooks, nextTick(), and scheduler jobs.
What it assumes
Assumes basic JavaScript functions, callbacks, promises, and asynchronous hooks while defining the execution stack and queue roles.
What you can do after
Evaluate asynchronous ordering and schedule lifecycle or update work using microtasks.
How it connects
Connects execution stack, task queue, microtask queue, promises, scheduler, lifecycle hooks, and DOM rendering.

Also explained in

Compare how 11 books explain event loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.