Challenge - 5 Problems
Event Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output order of this Node.js code?
Consider this Node.js code snippet. What will be the order of the printed lines in the console?
Node.js
console.log('Start'); setTimeout(() => console.log('Timeout'), 0); Promise.resolve().then(() => console.log('Promise')); console.log('End');
Attempts:
2 left
💡 Hint
Remember that promises run before timers even if the timer delay is zero.
✗ Incorrect
The synchronous logs run first: 'Start' then 'End'. Then microtasks like promises run before timers. So 'Promise' logs before 'Timeout'.
❓ state_output
intermediate2:00remaining
What is the final value of counter after this code runs?
Given this Node.js code, what is the final value of the variable
counter after all asynchronous tasks complete?Node.js
let counter = 0; setTimeout(() => { counter += 1; }, 0); Promise.resolve().then(() => { counter += 2; }); counter += 3;
Attempts:
2 left
💡 Hint
Synchronous code runs first, then microtasks, then timers.
✗ Incorrect
Initially, counter is 0. Then synchronous counter += 3 makes it 3. Promise microtask adds 2 making 5. Timer adds 1 last making 6, but timer runs after microtasks and the question asks after all async tasks complete, so final is 6.
📝 Syntax
advanced2:00remaining
Which option causes a SyntaxError in this async code?
Identify which code snippet will cause a SyntaxError when run in Node.js 20+.
Attempts:
2 left
💡 Hint
The await keyword must be followed by an expression.
✗ Incorrect
Option C uses await without an expression, which is invalid syntax and causes a SyntaxError.
🔧 Debug
advanced2:00remaining
Why does this code never print 'Done'?
Examine the code below. Why does it never print 'Done' to the console?
Node.js
setImmediate(() => {
while(true) {}
console.log('Done');
});Attempts:
2 left
💡 Hint
Think about what happens when the event loop is blocked.
✗ Incorrect
The infinite loop inside the setImmediate callback blocks the event loop, so the console.log('Done') line is never executed.
🧠 Conceptual
expert2:00remaining
Which statement best describes the Node.js event loop phases?
Select the option that correctly orders the main phases of the Node.js event loop.
Attempts:
2 left
💡 Hint
Remember the event loop starts with timers phase.
✗ Incorrect
The Node.js event loop phases run in this order: Timers, Pending Callbacks, Poll, Check, then Close Callbacks.