JavaScript’s Event Loop

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.lo…


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?

  • Start and End are synchronous → run immediately on the call stack.
  • Promise.then is a microtask → runs before macrotasks.
  • setTimeout callback 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
APA

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/

MLA
" » JavaScript’s Event Loop." Mohsen Fallahnejad | Sciencx - Saturday September 20, 2025, https://www.scien.cx/2025/09/20/javascripts-event-loop/
HARVARD
Mohsen Fallahnejad | Sciencx Saturday September 20, 2025 » JavaScript’s Event Loop., viewed ,<https://www.scien.cx/2025/09/20/javascripts-event-loop/>
VANCOUVER
Mohsen 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.

You must be logged in to translate posts. Please log in or register.