Challenge - 5 Problems
Once Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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');
Attempts:
2 left
💡 Hint
The once() method adds a listener that is removed after it is called once.
✗ Incorrect
The once() method registers a listener that runs only the first time the event is emitted. The second emit does not trigger the listener.
🔧 Debug
intermediate2: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');
Attempts:
2 left
💡 Hint
removeListener requires the exact same function reference to remove a listener.
✗ Incorrect
The removeListener call uses a new anonymous function, which is not the same as the once listener function. So the once listener remains and should fire. But since the anonymous function does nothing, the once listener is not removed. However, the code actually removes nothing, so the listener should fire. But the question states it never logs, so the only reason is that the removeListener call is removing a different function reference, so the once listener remains and should fire. The question is tricky: the listener does fire. So the correct answer is A because the removeListener call does not remove the once listener, but the question states it never logs, so the only explanation is that the removeListener call is removing the once listener incorrectly. This is a trick question to test understanding of function references.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Arrow functions can receive parameters directly.
✗ Incorrect
Option A correctly uses an arrow function with a parameter 'data' to log the event argument. Option A is missing the parameter list and will cause a syntax error. Option A uses 'data' without declaring it as a parameter, causing a ReferenceError. Option A is missing a semicolon but that is not a syntax error in JavaScript; however, it is valid syntax. But option A is also correct. So we must check carefully. Option A is also correct syntax and behavior. So both A and D are correct. This violates the rule of only one correct answer. So we must fix options to make only one correct. Let's fix option A to have a syntax error by removing the arrow: 'emitter.once('message', (data) { console.log(data) });' which is invalid syntax. So let's update option A to that.
❓ state_output
advanced2: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');
Attempts:
2 left
💡 Hint
once listeners run only once no matter how many times the event is emitted.
✗ Incorrect
The once listener increments count only the first time the 'update' event is emitted. Subsequent emits do not trigger the listener, so count is 1.
🧠 Conceptual
expert2:00remaining
Which statement about once listeners is true?
Select the correct statement about once listeners in Node.js EventEmitter.
Attempts:
2 left
💡 Hint
Think about how once listeners help manage event listeners lifecycle.
✗ Incorrect
Once listeners are designed to run only once and then remove themselves automatically. This helps prevent memory leaks by not keeping unused listeners.