tezvyn:

The JavaScript Event Loop: Asynchronicity on a Single Thread

Source: developer.mozilla.orgintermediate

The event loop is a queue that lets single-threaded JavaScript handle asynchronous tasks without blocking. It processes callbacks from Web APIs like `fetch` or `setTimeout` one at a time. The footgun: `setTimeout(fn, 0)` doesn't run instantly, just next.

The event loop is a queue that lets single-threaded JavaScript handle asynchronous tasks without blocking the main thread. It works with the call stack and a host environment (like a browser). Long-running operations like `fetch` are offloaded, and their callbacks are added to the queue to be executed once the stack is clear. The common footgun is assuming `setTimeout(fn, 0)` runs immediately; it only queues the function to run after the current synchronous code finishes.

Read the original → developer.mozilla.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

The JavaScript Event Loop: Asynchronicity on a Single Thread · Tezvyn