0
0
Node.jsframework~5 mins

Once listeners in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a 'once' listener in Node.js EventEmitter?
A 'once' listener is an event handler that runs only one time when the event is emitted, then it automatically removes itself.
Click to reveal answer
beginner
How do you add a 'once' listener to an event in Node.js?
Use the once method on an EventEmitter instance, like emitter.once('eventName', callback).
Click to reveal answer
intermediate
Why use a 'once' listener instead of a regular listener?
To handle an event only the first time it happens, avoiding repeated calls and automatically cleaning up the listener.
Click to reveal answer
beginner
What happens if you emit an event multiple times but have a 'once' listener attached?
The 'once' listener runs only on the first emit. Subsequent emits do not trigger it because it removes itself after the first call.
Click to reveal answer
intermediate
Can you remove a 'once' listener before it fires? How?
Yes, by using emitter.off('eventName', listenerFunction) or emitter.removeListener('eventName', listenerFunction) before the event is emitted.
Click to reveal answer
Which method adds a listener that runs only once in Node.js EventEmitter?
AaddListener()
Bonce()
Con()
Demit()
What happens to a 'once' listener after it is triggered?
AIt is removed automatically
BIt stays active for future events
CIt throws an error
DIt duplicates itself
How can you remove a 'once' listener before it fires?
AUse emitter.emit()
BYou cannot remove it
CUse emitter.off() or emitter.removeListener() with the listener function
DUse emitter.once() again
If an event is emitted three times but has a 'once' listener, how many times does the listener run?
A1 time
B2 times
C3 times
D0 times
Which of these is NOT true about 'once' listeners?
AThey run only once
BThey automatically remove themselves
CThey can be removed before firing
DThey can be reused multiple times without re-adding
Explain what a 'once' listener is and why you might use it in a Node.js application.
Think about events you want to handle only the first time they happen.
You got /4 concepts.
    Describe how to add and remove a 'once' listener on an EventEmitter instance.
    Focus on the methods and parameters involved.
    You got /4 concepts.