0
0
Node.jsframework~20 mins

Why timing matters in Node.js in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Timing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1: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?
ATo use multiple CPU cores automatically without extra code.
BTo make Node.js run slower but more securely.
CTo force all tasks to run one after another, blocking the program.
DTo allow Node.js to perform non-blocking operations and handle many tasks at once.
Attempts:
2 left
💡 Hint
Think about how Node.js handles many users or tasks without waiting for each to finish.
component_behavior
intermediate
1: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');
AOnly After setTimeout
B
Timeout done
After setTimeout
C
After setTimeout
Timeout done
DOnly Timeout done
Attempts:
2 left
💡 Hint
Remember that even 0 ms delay means the callback waits for the current code to finish.
state_output
advanced
2: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');
A
Start
End
Next Tick
Immediate
B
Start
Immediate
Next Tick
End
C
Start
Next Tick
End
Immediate
D
Next Tick
Start
End
Immediate
Attempts:
2 left
💡 Hint
process.nextTick runs before other asynchronous callbacks, but after current code.
📝 Syntax
advanced
1: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?
Aprocess.nextTick(() => console.log('After I/O events'));
BsetImmediate(() => console.log('After I/O events'));
CsetTimeout(() => console.log('After I/O events'), 0);
DPromise.resolve().then(() => console.log('After I/O events'));
Attempts:
2 left
💡 Hint
Which callback runs in the check phase of the event loop?
🔧 Debug
expert
2: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');
A'Done' is printed immediately because console.log runs synchronously.
B'Done' is never printed because wait() blocks the event loop forever.
C'Done' is printed after many recursive calls finish.
D'Done' is never printed because the program crashes with a stack overflow.
Attempts:
2 left
💡 Hint
Consider when console.log runs compared to setTimeout callbacks.