liveBook · Manning

Event loop

As explained in Grokking Simplicity

A runtime mechanism that repeatedly retrieves queued events or callbacks and invokes them when the call stack is available.

The event loop repeatedly takes one job from the job queue, runs its callback to completion, and then processes the next job. It runs on one thread, so jobs do not execute simultaneously. Each job combines event data with the callback that handles it. section 15.15

Keep these rules in mind:

  • Events such as clicks, typing, and AJAX completion add jobs unpredictably to the queue. section 15.15
  • Asynchronous work cannot normally return its eventual result; pass that result to a callback instead. section 15.30

Adding shoes to a cart

A click callback starts an AJAX request and changes the button to “loading.” When the request completes, another queued job runs its callback, updates the cart, renders the icon, and restores the button text. section 15.17

Image

Use the diagram to trace how queued events, callbacks, and the single-threaded event loop interact. section 15.15

How Grokking Simplicity covers it

How deep it goes
Defines jobs, callbacks, queue processing, and single-threaded execution, then traces a complete AJAX interaction.
Examples
Uses click handling, AJAX cart updates, nested dog-and-cat requests, and a dishwashing callback refactoring exercise.
What it assumes
Assumes basic JavaScript functions and callbacks while introducing queues, asynchronous timing, and callback-based results.
What you can do after
Apply it via JavaScript callbacks to structure asynchronous results instead of relying on immediate return values.
How it connects
Connects the job queue with callbacks, event data, the call stack, AJAX, and asynchronous return values.

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 Grokking Simplicity. The section links point to the parts of the book it draws on.