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?✗ Incorrect
The
on() method registers a listener function for an event.What does the
emit() method do?✗ 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?
✗ Incorrect
Emitting an event with no listeners does nothing; no functions run.
Which method removes a listener from an event?
✗ Incorrect
The
off() method removes a listener from an event.What kind of programming does
EventEmitter help manage?✗ 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.