Asynchronous programming allows a program to continue to run other codes while waiting for an already running task to complete. The running task is executed in the background while the rest of the code is prioritized.
In single-threaded environments such as JavaScript, asynchronous operations (timer delays, network requests, or file I/O) are offloaded to host environment APIs (for example, Browser Web APIs or Node.js C++ APIs). Once complete, the results are processed using callback functions passed as arguments to be executed later.
The Callback Queue also known as the Task Queue or Event Queue based on FIFO (First-In, First-Out) data structure that holds callbacks ready for execution.
When an asynchronous background task completes, its corresponding callback is moved into the Callback Queue, where it waits in line until the main execution thread is free to process it.
The Event Loop is the orchestration mechanism that connects the Call Stack and the Callback Queue.
It continuously monitors two conditions:
If the stack is empty, the Event Loop takes the first callback from the Callback Queue and pushes it onto the Call Stack, executing it safely without causing race conditions or blocking user interactions.

Algorithm(Pseudocode)
console.log('Starting app');
setTimeout(() => {
console.log('Inside of callback');
}, 2000);
setTimeout(() => {
console.log('Second setTimeout');
}, 0);
console.log('Finishing up');
Output:
Starting app Finishing up Second setTimeout Inside of callback

Explanation
Starting app runs synchronously and logs immediately. setTimeout(..., 2000) registers a timer with the Web API and sets a 2-second delay. setTimeout(..., 0) registers a 0ms timer. Even though its timer completes instantly, its callback is moved to the Callback Queue and must wait for the main thread to clear.
Finishing up logs synchronously. The Call Stack is now empty. The Event Loop pulls Second setTimeout from the queue and executes it. After 2 seconds, the first timer's callback arrives in the queue and is executed (Inside of callback).
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India