Complete the code to import the EventEmitter class from the 'events' module.
const [1] = require('events');
The EventEmitter class is the core of Node.js event system. Importing it correctly allows you to create event-driven code.
Complete the code to create a new instance of EventEmitter.
const emitter = new [1]();You create a new event emitter by calling new EventEmitter(). This instance can then emit and listen to events.
Fix the error in the code to listen for the 'message' event.
emitter.[1]('message', () => { console.log('Message received'); });
The correct method to listen for events in Node.js EventEmitter is on. It registers a callback for the named event.
Fill both blanks to emit a 'data' event with a payload.
emitter.[1]('[2]', { id: 1, value: 'hello' });
To send an event, use emit with the event name and data. Here, the event name is 'data'.
Fill all three blanks to create a listener for 'update' event that logs the payload's status.
emitter.[1]('[2]', (payload) => { console.log('Status:', payload.[3]); });
Use on to listen for the 'update' event. The callback receives a payload object, and we access its 'status' property.