2. It sends the async task to Web API
3. Callback stays associated with Web API while task is running
4. Task finishes (timer done / response received)
5. Callback moves to Message Queue
6. Event Loop checks: Is Call Stack empty?
7. If empty → callback moves from Message Queue to Call Stack
8. Callback executes
Example:
console.log("Start");
fetch("https://api.example.com/users")
.then(response => response.json())
.then(data => console.log(data));
console.log("End");What happens:
1. "Start" → executes in Call Stack.
2. fetch() is called.
3. Browser/Web API starts network request
(sending request to server).
4. JavaScript does NOT wait.
5. "End" executes in Call Stack.
6. Server sends response back.
7. Web API notices response arrived.
8. Callback (.then()) moves to Queue.
9. Event Loop checks stack.
10. If stack empty → callback goes to Call Stack.
11. callback executes → console.log(data).
No comments:
Post a Comment