Challenge - 5 Problems
Node.js Timing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why does Node.js use an event loop?
Node.js uses an event loop to handle tasks. What is the main reason for this design?
Attempts:
2 left
💡 Hint
Think about how Node.js handles many users or tasks without waiting for each to finish.
✗ Incorrect
Node.js uses an event loop to perform non-blocking operations, allowing it to handle many tasks concurrently without waiting for each to finish before starting the next.
❓ component_behavior
intermediate1:30remaining
What happens when you call setTimeout with 0 ms in Node.js?
Consider this code snippet in Node.js:
setTimeout(() => console.log('Timeout done'), 0);
console.log('After setTimeout');
What will be the order of the printed lines?
Node.js
setTimeout(() => console.log('Timeout done'), 0); console.log('After setTimeout');
Attempts:
2 left
💡 Hint
Remember that even 0 ms delay means the callback waits for the current code to finish.
✗ Incorrect
setTimeout with 0 ms schedules the callback after the current call stack is empty, so 'After setTimeout' prints first, then 'Timeout done'.
❓ state_output
advanced2:00remaining
What is the output of this asynchronous code?
Analyze this Node.js code:
console.log('Start');
setImmediate(() => console.log('Immediate'));
process.nextTick(() => console.log('Next Tick'));
console.log('End');
Node.js
console.log('Start'); setImmediate(() => console.log('Immediate')); process.nextTick(() => console.log('Next Tick')); console.log('End');
Attempts:
2 left
💡 Hint
process.nextTick runs before other asynchronous callbacks, but after current code.
✗ Incorrect
The synchronous logs 'Start' and 'End' run first. Then process.nextTick callback runs before setImmediate, so order is Start, End, Next Tick, Immediate.
📝 Syntax
advanced1:30remaining
Which code snippet correctly schedules a callback after I/O events in Node.js?
You want to run a function after I/O events callbacks. Which of these options is correct?
Attempts:
2 left
💡 Hint
Which callback runs in the check phase of the event loop?
✗ Incorrect
setImmediate callbacks run after I/O events in the event loop, making them suitable to run code after I/O callbacks.
🔧 Debug
expert2:00remaining
Why does this Node.js code never print 'Done'?
Look at this code:
function wait() {
setTimeout(() => {
wait();
}, 0);
}
wait();
console.log('Done');
Why is 'Done' never printed?
Node.js
function wait() {
setTimeout(() => {
wait();
}, 0);
}
wait();
console.log('Done');Attempts:
2 left
💡 Hint
Consider when console.log runs compared to setTimeout callbacks.
✗ Incorrect
console.log('Done') runs immediately after wait() is called because wait schedules callbacks asynchronously. The program does not block or crash, so 'Done' prints right away.