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
Using Once Listeners in Node.js EventEmitter
📖 Scenario: You are building a simple Node.js program that reacts to a special event only once. This is useful when you want to run some code the first time something happens, but not on later times.
🎯 Goal: Create an EventEmitter, add a once listener for a custom event called greet, and emit the event twice. The listener should run only the first time.
📋 What You'll Learn
Create an EventEmitter instance called emitter
Add a once listener for the event named greet that logs 'Hello once!'
Emit the greet event twice
Ensure the listener runs only once
💡 Why This Matters
🌍 Real World
Once listeners are useful in real applications where you want to respond to an event only the first time it happens, such as initializing a resource or handling a one-time setup.
💼 Career
Understanding event-driven programming and Node.js EventEmitter is essential for backend developers working with asynchronous code and real-time applications.
Progress0 / 4 steps
1
Create EventEmitter instance
Import the EventEmitter class from the events module and create an instance called emitter.
Node.js
Hint
Use require('events') to import EventEmitter and then create emitter with new EventEmitter().
2
Add a once listener for 'greet'
Add a once listener to emitter for the event 'greet' that logs 'Hello once!' when triggered.
Node.js
Hint
Use emitter.once('greet', callback) where the callback logs the message.
3
Emit the 'greet' event twice
Emit the 'greet' event two times using emitter.emit('greet').
Node.js
Hint
Call emitter.emit('greet') two times to trigger the event.
4
Complete the program
Ensure the program is complete with the import, emitter creation, once listener, and two emits as shown.
Node.js
Hint
Check that all parts are present: import, emitter, once listener, and two emits.
Practice
(1/5)
1. What is the main behavior of a once listener in Node.js event handling?
easy
A. It runs twice before removing itself automatically.
B. It runs only the first time the event is emitted and then removes itself.
C. It runs every time the event is emitted without removing itself.
D. It never runs automatically and must be triggered manually.
Solution
Step 1: Understand the purpose of once listeners
Once listeners are designed to handle an event only once, meaning they execute their callback a single time.
Step 2: Recognize automatic removal behavior
After running once, the listener removes itself automatically to prevent further executions.
Final Answer:
It runs only the first time the event is emitted and then removes itself. -> Option B
Quick Check:
Once listener = runs once then removes [OK]
Hint: Once listeners run once then auto-remove themselves [OK]
Common Mistakes:
Thinking once listeners run multiple times
Confusing once with on listeners
Assuming manual removal is needed
2. Which of the following is the correct syntax to add a once listener to an EventEmitter instance emitter for event 'start'?
easy
A. emitter.once('start', () => { console.log('Started'); });
B. emitter.on('start', () => { console.log('Started'); });
C. emitter.once('start', console.log('Started'));
D. emitter.once('start', function console.log('Started'));
Solution
Step 1: Identify the correct method for once listeners
The method to add a once listener is once, not on.
Step 2: Check the callback syntax
The callback must be a function, so () => { console.log('Started'); } is correct. Passing the result of console.log directly is wrong.
Final Answer:
emitter.once('start', () => { console.log('Started'); }); -> Option A
Quick Check:
Use emitter.once(event, callback) with a function [OK]
Hint: Use emitter.once with a function callback [OK]
A. The listener callback is called immediately, not on event emit.
B. The event name 'data' is invalid for once listeners.
C. The emitter.emit call is missing a callback function.
D. The once method does not exist on EventEmitter.
Solution
Step 1: Check how the callback is passed
The code passes console.log('Data event') which calls console.log immediately and passes its return (undefined) as listener.
Step 2: Understand correct callback usage
The callback should be a function, e.g., () => console.log('Data event'), to run only on event emit.
Final Answer:
The listener callback is called immediately, not on event emit. -> Option A
Quick Check:
Pass function, not function call, as listener [OK]
Hint: Pass function, not function call, as listener [OK]
Common Mistakes:
Calling function instead of passing it
Assuming event name invalid
Thinking once method is missing
5. You want to log a message only the first time a user connects to your server using an EventEmitter server. Which code snippet correctly achieves this and prevents multiple logs if the user reconnects?
hard
A. server.on('connect', () => console.log('User connected'));
B. server.on('connect', console.log('User connected'));
C. server.once('connect', console.log('User connected'));
D. server.once('connect', () => console.log('User connected'));
Solution
Step 1: Identify the requirement for single execution
Logging only once means the listener should run once and then remove itself.
Step 2: Choose the correct method and callback syntax
server.once('connect', () => console.log('User connected')) uses the once method with a function callback, ensuring single log on first connect.
Final Answer:
server.once('connect', () => console.log('User connected')); -> Option D
Quick Check:
Use once with function callback for single event handling [OK]
Hint: Use once with function callback to run once [OK]