0
0
Node.jsframework~10 mins

Once listeners in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Once listeners
Create EventEmitter
Add once listener
Emit event
Listener runs
Listener removed automatically
Emit event again
No listener runs
END
This flow shows how a once listener is added, runs only once when the event emits, then is removed automatically.
Execution Sample
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.once('greet', () => console.log('Hello!'));
emitter.emit('greet');
emitter.emit('greet');
This code adds a once listener to 'greet' event, emits it twice, but the listener runs only the first time.
Execution Table
StepActionListener Count for 'greet'OutputNotes
1Create EventEmitter instance0No listeners yet
2Add once listener for 'greet'1Listener added, will run once
3Emit 'greet' event first time0Hello!Listener runs and then is removed
4Listener removed automatically0No listeners remain for 'greet'
5Emit 'greet' event second time0No listener runs this time
💡 No listeners remain after first emit, so second emit does nothing
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
emitter.listenerCount('greet')01000
Key Moments - 2 Insights
Why does the listener not run the second time the event is emitted?
Because the listener was added with 'once', it automatically removes itself after running once, as shown in execution_table step 4.
Does the listener get removed before or after it runs on the first emit?
The listener runs first, then is removed immediately after, as shown in execution_table step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the listener count for 'greet' after the first emit?
A1
B2
C0
DCannot tell
💡 Hint
Check the 'Listener Count for greet' column at step 4 in the execution table.
At which step does the listener get removed automatically?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Notes' column describing listener removal in the execution table.
If we replaced 'once' with 'on', what would happen on the second emit?
AListener runs again
BListener runs only once
CListener is removed automatically
DNo listener runs
💡 Hint
Refer to the concept flow where 'once' removes listener after first run; 'on' keeps it.
Concept Snapshot
EventEmitter.once(event, listener) adds a listener that runs only once.
When the event emits, the listener runs and is removed automatically.
Subsequent emits of the same event do not trigger the listener.
Use once for one-time event handling.
Listener count decreases after first emit.
Full Transcript
In Node.js, the EventEmitter class allows adding listeners that respond to events. Using the 'once' method adds a listener that runs only the first time the event is emitted. When the event is emitted, the listener runs and then removes itself automatically. If the event is emitted again, the listener does not run because it no longer exists. This behavior is useful for handling events that should only trigger a single time. The execution table shows the listener count starting at zero, increasing to one when added, running on the first emit, then dropping back to zero after removal. The second emit finds no listener and produces no output.