Complete the code to create an event emitter instance.
const EventEmitter = require('events'); const emitter = new [1]();
The EventEmitter class is used to create event emitter instances in Node.js.
Complete the code to add a callback function for the 'data' event.
emitter.on('data', [1] => { console.log('Data received:', data); });
The callback function receives the event data as a parameter. Using data as the parameter name is common and clear.
Fix the error in the callback usage by completing the code.
function fetchData(callback) {
setTimeout(() => {
const result = 'Hello';
callback([1]);
}, 1000);
}The callback function should be called with the data result as argument.
Fill both blanks to emit an event with a message and listen for it.
emitter.[1]('message', 'Hi there!'); emitter.[2]('message', msg => { console.log(msg); });
Use emit to send an event and on to listen for it.
Fill all three blanks to decide between callback and event for async handling.
function processData(data, [1]) { if (data.length > 5) { [2]('error', new Error('Too long')); } else { [3](null, data); } }
Use a callback function to handle success or error. Use emitter.emit to send an error event.
