This content originally appeared on DEV Community and was authored by Mohsen Fallahnejad
The event loop lets JavaScript handle async tasks on a single thread.
Example Code
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
Promise.resolve().then(() => {
console.log("Promise");
});
console.log("End");
Output: Start → End → Promise → Timeout
Why this order?
-
StartandEndare synchronous → run immediately on the call stack. -
Promise.thenis a microtask → runs before macrotasks. -
setTimeoutcallback is a macrotask → runs after microtasks drain.
Originally published on: Bitlyst
This content originally appeared on DEV Community and was authored by Mohsen Fallahnejad
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Mohsen Fallahnejad | Sciencx (2025-09-20T16:41:17+00:00) JavaScript’s Event Loop. Retrieved from https://www.scien.cx/2025/09/20/javascripts-event-loop/
" » JavaScript’s Event Loop." Mohsen Fallahnejad | Sciencx - Saturday September 20, 2025, https://www.scien.cx/2025/09/20/javascripts-event-loop/
HARVARDMohsen Fallahnejad | Sciencx Saturday September 20, 2025 » JavaScript’s Event Loop., viewed ,<https://www.scien.cx/2025/09/20/javascripts-event-loop/>
VANCOUVERMohsen Fallahnejad | Sciencx - » JavaScript’s Event Loop. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/20/javascripts-event-loop/
CHICAGO" » JavaScript’s Event Loop." Mohsen Fallahnejad | Sciencx - Accessed . https://www.scien.cx/2025/09/20/javascripts-event-loop/
IEEE" » JavaScript’s Event Loop." Mohsen Fallahnejad | Sciencx [Online]. Available: https://www.scien.cx/2025/09/20/javascripts-event-loop/. [Accessed: ]
rf:citation » JavaScript’s Event Loop | Mohsen Fallahnejad | Sciencx | https://www.scien.cx/2025/09/20/javascripts-event-loop/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.