0
0
Node.jsframework~20 mins

Why the event system matters in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event System Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use an event-driven system in Node.js?

Node.js uses an event-driven system. What is the main benefit of this design?

AIt allows Node.js to handle many tasks at the same time without waiting for each to finish.
BIt makes Node.js run slower because it waits for events to happen.
CIt forces Node.js to use multiple CPU cores automatically.
DIt stops Node.js from running any code until all events are processed.
Attempts:
2 left
💡 Hint

Think about how Node.js can manage many users or tasks without delays.

component_behavior
intermediate
2:00remaining
What happens when an event is emitted?

In Node.js, when an event is emitted, what happens next?

Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', () => console.log('Hello!'));
emitter.emit('greet');
AThe event is ignored because no callback is provided.
BNothing happens because events need to be awaited.
CThe program crashes because 'greet' is not a built-in event.
DThe 'greet' event triggers the listener and prints 'Hello!' to the console.
Attempts:
2 left
💡 Hint

Look at what the listener does when the event is emitted.

🔧 Debug
advanced
2:00remaining
Why does this event listener not run?

Look at this code. Why does the listener not print anything?

Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.emit('start');
emitter.on('start', () => console.log('Started!'));
AThe event is emitted before the listener is added, so the listener misses it.
BThe listener function has a syntax error and does not run.
CThe event name 'start' is invalid and ignored.
DNode.js does not support multiple listeners for the same event.
Attempts:
2 left
💡 Hint

Think about the order of adding listeners and emitting events.

state_output
advanced
2:00remaining
What is the output of this event count code?

What will this code print?

Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('ping', () => count++);
emitter.emit('ping');
emitter.emit('ping');
console.log(count);
Aundefined
B1
C2
D0
Attempts:
2 left
💡 Hint

Count how many times the 'ping' event is emitted and the listener runs.

📝 Syntax
expert
3:00remaining
Which option correctly creates a custom event emitter class?

Choose the correct way to create a class that emits a 'ready' event when started.

Aclass MyEmitter extends require('events') { start() { this.emit('ready'); } }
Bconst EventEmitter = require('events'); class MyEmitter extends EventEmitter { start() { this.emit('ready'); } }
Cconst EventEmitter = require('events'); function MyEmitter() { this.emit('ready'); }
Dclass MyEmitter { constructor() { this.emit('ready'); } }
Attempts:
2 left
💡 Hint

Remember how to extend EventEmitter properly in Node.js.