Summary - Event loop internals
- Node.js course
- Expert
- Lesson 18/47
- 5 min
Combine phases, microtasks and I/O to write an event loop visualizer.
Summary - Event loop internals
In this group we saw the internals of the event loop: the six phases, microtasks, the timers queue, I/O callbacks and setImmediate. Each determines when callbacks run.
Phases: timers, pending, idle, poll, check, close. Microtasks run between phases; nextTick first. The timers queue orders by delay. I/O callbacks run in the poll phase. setImmediate runs in the check phase. In the final lesson we combine them in a report.
The report function
The final task combines event loop concepts into one report. The loopReport() function joins the phase count and execution order with ' | '. Output: Phases: 6 | Order: timer -> io -> immediate.
This report summarizes two aspects of the event loop: how many phases (6) and the execution order in one turn (timer -> io -> immediate). Inside the function you keep the phases array and the order string in separate variables, then join them with +. phases.length gives the phase count. This lets you see what you learned on the console; you model the structure without the uncertainty of running the real event loop.
Practice Write the event loop report
Write function loopReport(). Define const phases = ['timers', 'pending', 'idle', 'poll', 'check', 'close']. Define const order = ['timer', 'io', 'immediate'].join(' -> '). Return 'Phases: ' + phases.length + ' | Order: ' + order. Print console.log(loopReport()).
Starter code
// Write a loopReport function
// Combine the phase count and execution order
// Return 'Phases: 6 | Order: timer -> io -> immediate'
// Call and print
// write hereSample solution
function loopReport() {
const phases = ['timers', 'pending', 'idle', 'poll', 'check', 'close'];
const order = ['timer', 'io', 'immediate'].join(' -> ');
return 'Phases: ' + phases.length + ' | Order: ' + order;
}
console.log(loopReport());Expected output
Phases: 6 | Order: timer -> io -> immediate
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When do microtasks run?
- Between phases Correct answer
- Only at the start
- Inside the phases
- Sadece sonda
Explanation: Microtasks run between phases.
2 Match each concept to its role
| Term | Meaning |
|---|---|
timers phase |
setTimeout |
poll phase |
I/O callbacks |
check phase |
setImmediate |
microtask |
nextTick and Promise |