JavaScriptQuestion 11 of 36
What do you understand about event loop?
Answer out loud or in your head first, then compare.
Answer
The event loop coordinates synchronous and asynchronous code.
The key parts:
Call Stack
Microtask Queue
Task / Macrotask QueueExample:
JavaScript
console.log("A");
setTimeout(() => {
console.log("B");
}, 0);
Promise.resolve().then(() => {
console.log("C");
});
console.log("D");Output:
A
D
C
BWhy?
First, all the synchronous code runs:
A
DThe Promise callback goes to the microtask queue.
The setTimeout callback goes to the task/macrotask queue.
Once the call stack is empty, the event loop runs the microtasks first.
That's why:
Promisecomes before:
setTimeoutTo be precise:
A Promise doesn't run .then() immediately either. Its callback also waits, just in a higher-priority queue.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- B-P03 · What is the event loop? — JavaScript, TypeScript and Node.js
- The event loop in Node.js — Node.js