Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.57

bookThe Callback Queue

Swipe to show menu

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
some-alt