---
title: "Chapter 9. The Event Loop"
url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch09"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18T12:24:57.879Z"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
access: "lead-in"
purchase: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch09"
    markdown: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch09.md"
  - locale: "en"
    url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch09"
    markdown: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch09.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch09"
    markdown: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch09.md"
---

# Chapter 9. The Event Loop

*[JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)*

JavaScript in the browser always had one thread, and nobody intended to give it a second. A page lived like this: the browser waited for something to happen, and when something did, say the user clicked a button or a response came from the server, it called the right function and waited for it to finish. While the function ran, nothing else happened on the page. Then the browser took the next event from the queue, and so on round and round. That loop is what is called the event loop. The language itself knows nothing about it: as the first chapter said, the loop is described in the HTML standard, and Node has its own.

The design is primitive, but it has a great virtue. Since nobody interrupts a function once it has started, nobody can change shared data in the middle of its work. To a Java or C++ programmer this will seem a miracle. There, two threads incrementing the same counter at once can lose half the increments, so shared data is guarded by locks, and locks have their own troubles: deadlocks, where two threads wait for each other forever, and bugs that show up once a month on a loaded server and never on the developer’s machine. In JavaScript this whole class of bugs does not exist within one piece of code, because only one piece runs at a time.

The drawback is just as clear. If a function computes for a long time, the page freezes. The older generation remembers the «A script on this page is not responding» dialog, which the browser showed when some script held the loop for too long, asking whether to kill it.

---

The rest of this chapter is free on the site once the reader confirms an email address: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch09
