0
0
Node.jsframework~5 mins

Custom event emitter classes in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom event emitter class in Node.js?
A custom event emitter class is a user-defined class that extends Node.js's built-in EventEmitter to create and manage custom events, allowing different parts of an app to communicate by emitting and listening to events.
Click to reveal answer
beginner
How do you create a custom event emitter class in Node.js?
You create a custom event emitter class by importing { EventEmitter } from the 'events' module and extending it with your class. Then you can add methods that emit events using this.emit('eventName').
Click to reveal answer
intermediate
Why use custom event emitter classes instead of plain callbacks?
Custom event emitters allow multiple listeners for the same event, better organization of event logic, and decoupling of code parts. Callbacks are limited to single responses and can get messy with many events.
Click to reveal answer
beginner
What method do you use to listen for an event in a custom event emitter class?
You use the .on('eventName', callback) method to listen for an event. The callback runs whenever the event is emitted.
Click to reveal answer
intermediate
How can you remove an event listener from a custom event emitter?
You can remove a listener using the .off('eventName', callback) method or .removeListener('eventName', callback). This stops the callback from running when the event fires.
Click to reveal answer
Which Node.js module do you extend to create a custom event emitter class?
Ahttp
Bevents
Cfs
Dstream
What method do you call to emit an event in a custom event emitter?
Aemit()
Bon()
Clisten()
Dtrigger()
How do you add a listener for an event named 'data'?
Aon('data', callback)
Bemit('data', callback)
Clisten('data', callback)
DaddListener('callback', 'data')
What happens if multiple listeners are attached to the same event?
AOnly the last listener runs
BOnly the first listener runs
CAll listeners run in the order they were added
DListeners run randomly
Which method removes a specific listener from an event?
AaddListener()
Bemit()
Con()
Doff()
Explain how to create and use a custom event emitter class in Node.js.
Think about how classes and events work together.
You got /5 concepts.
    Describe the benefits of using custom event emitter classes over simple callbacks.
    Consider how events help different parts of an app talk to each other.
    You got /4 concepts.