Challenge - 5 Problems
Listener Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens after removing a listener?
Consider this Node.js EventEmitter code. What will be the output after the listener is removed and the event is emitted again?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); function greet() { console.log('Hello!'); } emitter.on('sayHello', greet); emitter.emit('sayHello'); emitter.removeListener('sayHello', greet); emitter.emit('sayHello');
Attempts:
2 left
💡 Hint
Think about what happens when you remove a listener before emitting the event again.
✗ Incorrect
The first emit triggers the listener and prints 'Hello!'. After removing the listener, the second emit has no listeners, so no output occurs.
📝 Syntax
intermediate2:00remaining
Identify the syntax error when removing a listener
Which option contains a syntax error when trying to remove a listener from an EventEmitter?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); function onData() { console.log('Data received'); } emitter.on('data', onData); emitter.removeListener('data', ???);
Attempts:
2 left
💡 Hint
Remember that removeListener expects the exact function reference, not the result of calling it.
✗ Incorrect
Option A calls onData() immediately and passes its return value (undefined) to removeListener, causing a runtime error or no removal. The others pass a function reference or invalid types but only A is a syntax/runtime misuse.
🔧 Debug
advanced2:00remaining
Why does the listener not get removed?
Given this code, why does the listener not get removed as expected?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.on('ping', () => console.log('Ping received')); emitter.removeListener('ping', () => console.log('Ping received')); emitter.emit('ping');
Attempts:
2 left
💡 Hint
Think about how JavaScript treats function expressions and references.
✗ Incorrect
Each arrow function is a new function object. The listener passed to removeListener is not the same reference as the one added, so it does not remove the original listener.
❓ state_output
advanced2:00remaining
How many listeners remain after these operations?
After running this code, how many listeners remain for the 'update' event?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); function listener1() {} function listener2() {} emitter.on('update', listener1); emitter.on('update', listener2); emitter.removeListener('update', listener1); emitter.removeAllListeners('update'); emitter.on('update', listener1);
Attempts:
2 left
💡 Hint
Remember what removeAllListeners does and what happens after it.
✗ Incorrect
removeAllListeners('update') removes all listeners for 'update'. Then listener1 is added again, so only one listener remains.
🧠 Conceptual
expert2:00remaining
What error occurs when removing a listener with wrong function reference?
What error or behavior occurs if you call removeListener with a function that was never added as a listener?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); function foo() {} function bar() {} emitter.on('event', foo); emitter.removeListener('event', bar); emitter.emit('event');
Attempts:
2 left
💡 Hint
Consider what happens if you try to remove a listener that does not exist.
✗ Incorrect
removeListener silently does nothing if the function is not found. The original listener remains and is called on emit.