Node.js uses an event-driven system. What is the main benefit of this design?
Think about how Node.js can manage many users or tasks without delays.
Node.js uses events to handle many tasks without blocking. This means it can start a task, then move on to others while waiting for the first to finish, making it efficient.
In Node.js, when an event is emitted, what happens next?
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.on('greet', () => console.log('Hello!')); emitter.emit('greet');
Look at what the listener does when the event is emitted.
When 'greet' is emitted, the listener runs and prints 'Hello!'. Events trigger their listeners immediately.
Look at this code. Why does the listener not print anything?
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.emit('start'); emitter.on('start', () => console.log('Started!'));
Think about the order of adding listeners and emitting events.
Listeners must be added before the event is emitted. Here, the event fires before the listener exists, so nothing happens.
What will this code print?
const EventEmitter = require('events'); const emitter = new EventEmitter(); let count = 0; emitter.on('ping', () => count++); emitter.emit('ping'); emitter.emit('ping'); console.log(count);
Count how many times the 'ping' event is emitted and the listener runs.
The listener runs twice, increasing count each time, so the final count is 2.
Choose the correct way to create a class that emits a 'ready' event when started.
Remember how to extend EventEmitter properly in Node.js.
Option B correctly imports EventEmitter, extends it, and emits 'ready' inside a method.