0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Listener Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
A(no output)\n(no output)
BHello!\n(no output)
C(no output)\nHello!
DHello!\nHello!
Attempts:
2 left
💡 Hint
Think about what happens when you remove a listener before emitting the event again.
📝 Syntax
intermediate
2: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', ???);
Aemitter.removeListener('data', onData());
Bemitter.removeListener('data', onData);
Cemitter.removeListener('data', function onData() {});
Demitter.removeListener('data', 'onData');
Attempts:
2 left
💡 Hint
Remember that removeListener expects the exact function reference, not the result of calling it.
🔧 Debug
advanced
2: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');
ABecause the arrow functions are different instances, so removeListener does not match the original listener.
BBecause removeListener requires the event name to be uppercase.
CBecause the listener was never added due to a typo in the event name.
DBecause removeListener only works with named functions, not arrow functions.
Attempts:
2 left
💡 Hint
Think about how JavaScript treats function expressions and references.
state_output
advanced
2: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);
A2
B0
C1
D3
Attempts:
2 left
💡 Hint
Remember what removeAllListeners does and what happens after it.
🧠 Conceptual
expert
2: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');
ARemoves all listeners for 'event' regardless of function.
BThrows a TypeError because 'bar' was never added.
CThrows a ReferenceError because 'bar' is not defined.
DNo error; the original listener 'foo' remains and is called on emit.
Attempts:
2 left
💡 Hint
Consider what happens if you try to remove a listener that does not exist.