0
0
Node.jsframework~20 mins

Events vs callbacks decision in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event & Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to prefer events over callbacks?
In Node.js, which scenario best justifies using events instead of callbacks?
AWhen you want to execute a single asynchronous task and get its result once.
BWhen you need to handle multiple asynchronous actions that can happen at different times independently.
CWhen you want to block the main thread until a task finishes.
DWhen you want to write synchronous code without any asynchronous behavior.
Attempts:
2 left
💡 Hint
Think about how events allow multiple listeners and callbacks handle one response.
component_behavior
intermediate
2:00remaining
Output of event emitter with multiple listeners
What will be printed when this Node.js code runs?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('ping', () => console.log('First listener'));
emitter.on('ping', () => console.log('Second listener'));
emitter.emit('ping');
ANo output
BFirst listener
CSecond listener
DFirst listener\nSecond listener
Attempts:
2 left
💡 Hint
Remember that multiple listeners can respond to the same event.
📝 Syntax
advanced
2:00remaining
Identify the error in callback usage
What error does this code produce?
Node.js
function fetchData(callback) {
  setTimeout(() => {
    callback(null, 'data');
  }, 100);
}

fetchData((error, result) => {
  if (error) {
    console.log('Error:', error);
  } else {
    console.log('Result:', result);
  }
});

fetchData('not a function');
ATypeError: callback is not a function
BSyntaxError: Unexpected token
CNo error, prints 'Result: data' twice
DReferenceError: callback is not defined
Attempts:
2 left
💡 Hint
Check what happens when a non-function is passed as callback.
state_output
advanced
2:00remaining
State after event and callback usage
What is the value of the variable count after this code runs?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;

emitter.on('increment', () => { count += 1; });

function doWork(callback) {
  emitter.emit('increment');
  callback();
}

doWork(() => { count += 10; });
A11
B10
C0
D1
Attempts:
2 left
💡 Hint
Consider the order of event emission and callback execution.
🔧 Debug
expert
2:00remaining
Why does the listener not run on the second emit?
Given this code, why does the second `emitter.emit('data')` not print anything?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('data', () => console.log('Data received'));

emitter.emit('data');
emitter.emit('data');
AThe listener is removed before any emit happens.
BThe listener is never registered because of a syntax error.
CThe listener runs only once, so the second emit does nothing.
DThe event name is misspelled in the emit calls.
Attempts:
2 left
💡 Hint
Check how the 'once' method works for event listeners.