0
0
Node.jsframework~20 mins

Once listeners in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Once Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an event listener is added with once()?
Consider the following Node.js code using an EventEmitter. What will be the output after emitting the event twice?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('ping', () => {
  console.log('Ping received');
});

emitter.emit('ping');
emitter.emit('ping');
A
Ping received
Ping received
BPing received
CNo output
DError: Event listener called twice
Attempts:
2 left
💡 Hint
The once() method adds a listener that is removed after it is called once.
🔧 Debug
intermediate
2:00remaining
Why does this once listener not fire?
Look at this code snippet. Why does the listener never log anything?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

function onData() {
  console.log('Data received');
}

emitter.once('data', onData);

// Later in code
emitter.removeListener('data', () => {
  console.log('Data received');
});
emitter.emit('data');
ABecause removeListener uses a different function reference, so the once listener is removed before emit
BBecause once listeners never fire if removeListener is called
CBecause the event name 'data' is misspelled
DBecause emit is called before once listener is added
Attempts:
2 left
💡 Hint
removeListener requires the exact same function reference to remove a listener.
📝 Syntax
advanced
2:00remaining
Which option correctly adds a once listener that logs the event argument?
You want to add a once listener to an EventEmitter that logs the data passed with the event. Which code is correct?
Aemitter.once('message', (data) => console.log(data));
Bemitter.once('message', function { console.log(data); });
Cemitter.once('message', () => { console.log(data); });
Demitter.once('message', (data) => { console.log(data) });
Attempts:
2 left
💡 Hint
Arrow functions can receive parameters directly.
state_output
advanced
2:00remaining
What is the value of count after emitting an event with a once listener?
Given this code, what is the value of count after emitting 'update' event three times?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;

emitter.once('update', () => {
  count += 1;
});

emitter.emit('update');
emitter.emit('update');
emitter.emit('update');
A3
BError: count is not defined
C0
D1
Attempts:
2 left
💡 Hint
once listeners run only once no matter how many times the event is emitted.
🧠 Conceptual
expert
2:00remaining
Which statement about once listeners is true?
Select the correct statement about once listeners in Node.js EventEmitter.
AOnce listeners must be manually removed using removeListener after being called.
BA once listener can be called multiple times if the event is emitted rapidly.
CA once listener is automatically removed after it is called once, preventing memory leaks.
DOnce listeners do not receive event arguments.
Attempts:
2 left
💡 Hint
Think about how once listeners help manage event listeners lifecycle.