Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the <code>EventEmitter</code> class in Node.js?
The <code>EventEmitter</code> class allows objects to emit named events and register functions (listeners) to respond when those events occur. It helps manage asynchronous events easily.
Click to reveal answer
beginner
How do you add a listener function to an event using EventEmitter?
Use the on(eventName, listener) method to register a listener function that runs whenever the specified event is emitted.
Click to reveal answer
beginner
What method do you use to trigger or emit an event in EventEmitter?
Use the emit(eventName, ...args) method to trigger an event and optionally pass arguments to the listener functions.
Click to reveal answer
intermediate
What happens if you emit an event with no listeners attached in EventEmitter?
Nothing happens; the event is emitted but no functions run because no listeners are registered for that event.
Click to reveal answer
intermediate
How can you remove a listener from an event in EventEmitter?
Use the removeListener(eventName, listener) or off(eventName, listener) method to remove a specific listener function from an event.
Click to reveal answer
Which method adds a listener to an event in EventEmitter?
AremoveListener()
Bemit()
Ctrigger()
Don()
✗ Incorrect
The on() method registers a listener function for an event.
What does the emit() method do?
ATriggers an event
BRemoves a listener
CCreates a new EventEmitter
DLists all events
✗ Incorrect
The emit() method triggers an event and calls all listeners for that event.
If no listeners are registered for an event, what happens when you emit it?
AA default listener runs
BAn error is thrown
CNothing happens
DThe program crashes
✗ Incorrect
Emitting an event with no listeners does nothing; no functions run.
Which method removes a listener from an event?
Aemit()
Boff()
CaddListener()
Dlisten()
✗ Incorrect
The off() method removes a listener from an event.
What kind of programming does EventEmitter help manage?
AAsynchronous event-driven programming
BFunctional programming
CProcedural programming
DSynchronous programming
✗ Incorrect
EventEmitter helps manage asynchronous event-driven programming by handling events and listeners.
Explain how you would use the EventEmitter class to respond to a custom event in Node.js.
Think about how events and listeners work together.
You got /4 concepts.
Describe the difference between the on() and emit() methods in the EventEmitter class.
One sets up a reaction, the other starts the event.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the EventEmitter class in Node.js?
easy
A. To manage file system operations
B. To handle HTTP requests and responses
C. To allow parts of a program to send and listen for events
D. To create and manage database connections
Solution
Step 1: Understand the role of EventEmitter
The EventEmitter class is designed to let different parts of a program communicate by sending and listening for events.
Step 2: Compare with other options
Options B, C, and D relate to other Node.js modules like HTTP, FS, and database modules, not EventEmitter.
Final Answer:
To allow parts of a program to send and listen for events -> Option C
Quick Check:
EventEmitter = event communication [OK]
Hint: EventEmitter is about events, not HTTP or files [OK]
Common Mistakes:
Confusing EventEmitter with HTTP or file system modules
Thinking EventEmitter manages databases
Assuming EventEmitter runs code synchronously
2. Which of the following is the correct way to listen for an event named data using an EventEmitter instance called emitter?
easy
A. emitter.on('data', callback)
B. emitter.listen('data', callback)
C. emitter.emit('data', callback)
D. emitter.addEvent('data', callback)
Solution
Step 1: Identify the method to listen for events
The on method is used to register a callback to listen for a named event.
Step 2: Check other methods
emit sends events, not listens. listen and addEvent are not valid EventEmitter methods.
Final Answer:
emitter.on('data', callback) -> Option A
Quick Check:
Listen with on() = emitter.on('data', callback) [OK]
Hint: Use on() to listen, emit() to send events [OK]
Common Mistakes:
Using emit() to listen instead of send
Using non-existent methods like listen()
Confusing method names for event handling
3. What will be the output of the following code?
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', name => {
console.log(`Hello, ${name}!`);
});
emitter.emit('greet', 'Alice');
medium
A. greet Alice
B. No output
C. Error: greet event not found
D. Hello, Alice!
Solution
Step 1: Understand event registration
The code registers a listener for the 'greet' event that prints a greeting with the given name.
Step 2: Understand event emission
When emit('greet', 'Alice') runs, it calls the listener with 'Alice', printing "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option D
Quick Check:
emit triggers on() callback [OK]
Hint: emit calls on() listeners with arguments [OK]
B. The event listener is registered after emitting the event
C. emit() requires a callback function as second argument
D. The EventEmitter class is not imported correctly
Solution
Step 1: Check event listener registration timing
The listener for 'start' is added after the event is emitted, so it misses the event.
Step 2: Verify other options
EventEmitter is imported correctly, emit() does not require a callback, and 'start' is a valid event name.
Final Answer:
The event listener is registered after emitting the event -> Option B
Quick Check:
Listeners must be added before emit() [OK]
Hint: Add listeners before emitting events [OK]
Common Mistakes:
Emitting before listener registration
Thinking emit needs a callback argument
Assuming event names have restrictions
5. You want to create an EventEmitter that counts how many times an event ping is emitted and logs the count each time. Which code correctly implements this?
hard
A. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', () => {
count++;
console.log(`Ping count: ${count}`);
});
emitter.emit('ping');
emitter.emit('ping');
B. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.emit('ping', () => {
count++;
console.log(`Ping count: ${count}`);
});
C. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', count++);
emitter.emit('ping');
D. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', () => count++);
emitter.emit('ping');
console.log(`Ping count: ${count}`);
Solution
Step 1: Understand event counting logic
We need to increase count and log it inside the event listener each time 'ping' is emitted.
Step 2: Analyze each option
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', () => {
count++;
console.log(`Ping count: ${count}`);
});
emitter.emit('ping');
emitter.emit('ping'); correctly registers a listener that increments and logs count on each 'ping'. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.emit('ping', () => {
count++;
console.log(`Ping count: ${count}`);
}); wrongly uses emit with a callback. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', count++);
emitter.emit('ping'); passes count++ directly, which is incorrect. const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', () => count++);
emitter.emit('ping');
console.log(`Ping count: ${count}`); increments count but logs outside the event, so only logs once.
Final Answer:
Option A code correctly counts and logs on each ping event -> Option A
Quick Check:
Increment and log inside on() callback [OK]
Hint: Increment count inside on() callback, not outside [OK]
Common Mistakes:
Passing callback to emit() instead of on()
Incrementing count outside event listener
Using count++ directly as listener instead of function