Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer The Callback Queue | Foundations of Asynchronous JavaScript
Asynchronous JavaScript Explained

bookThe Callback Queue

To truly understand asynchronous JavaScript, you need to grasp how the callback queue works and how it interacts with the event loop. The callback queue is a place where JavaScript stores functions that are ready to run after asynchronous operations complete, such as timers or user events. When you use functions like setTimeout, their callbacks do not execute immediately. Instead, once their timer expires, their callback is placed in the callback queue. However, these callbacks are not executed right away—they must wait for the JavaScript engine's call stack to be empty. The event loop constantly checks if the call stack is clear; if it is, it takes the first function from the callback queue and pushes it onto the call stack to execute.

script.js

script.js

index.html

index.html

copy

In the example above, both setTimeout calls have a delay of 0 milliseconds. Many people expect their callbacks to run immediately after being scheduled, but that is not the case. Instead, both callbacks are placed in the callback queue, waiting for the call stack to be empty. Only after all synchronous code runs—so after "Start" and "End" are logged—does the event loop pull the callbacks from the queue and execute them one by one. This order ensures that all synchronous tasks are completed before any callbacks from the queue are run.

A common misconception is that a callback in the queue executes instantly once its timer ends, or that it can interrupt synchronous code. In reality, the callback queue is strictly FIFO (First-In, First-Out), and callbacks only run when the call stack is empty. This is why even a setTimeout with 0 milliseconds delay does not interrupt or precede synchronous code—it simply waits its turn.

question mark

When does a function in the callback queue get executed?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 6

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 3.57

bookThe Callback Queue

Veeg om het menu te tonen

To truly understand asynchronous JavaScript, you need to grasp how the callback queue works and how it interacts with the event loop. The callback queue is a place where JavaScript stores functions that are ready to run after asynchronous operations complete, such as timers or user events. When you use functions like setTimeout, their callbacks do not execute immediately. Instead, once their timer expires, their callback is placed in the callback queue. However, these callbacks are not executed right away—they must wait for the JavaScript engine's call stack to be empty. The event loop constantly checks if the call stack is clear; if it is, it takes the first function from the callback queue and pushes it onto the call stack to execute.

script.js

script.js

index.html

index.html

copy

In the example above, both setTimeout calls have a delay of 0 milliseconds. Many people expect their callbacks to run immediately after being scheduled, but that is not the case. Instead, both callbacks are placed in the callback queue, waiting for the call stack to be empty. Only after all synchronous code runs—so after "Start" and "End" are logged—does the event loop pull the callbacks from the queue and execute them one by one. This order ensures that all synchronous tasks are completed before any callbacks from the queue are run.

A common misconception is that a callback in the queue executes instantly once its timer ends, or that it can interrupt synchronous code. In reality, the callback queue is strictly FIFO (First-In, First-Out), and callbacks only run when the call stack is empty. This is why even a setTimeout with 0 milliseconds delay does not interrupt or precede synchronous code—it simply waits its turn.

question mark

When does a function in the callback queue get executed?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 6
some-alt