0
0
Node.jsframework~20 mins

Custom event emitter classes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom event emitter code?

Consider this Node.js custom event emitter class. What will be printed when the code runs?

Node.js
import { EventEmitter } from 'node:events';

class MyEmitter extends EventEmitter {
  constructor() {
    super();
    this.on('ping', () => {
      console.log('pong');
    });
  }
}

const emitter = new MyEmitter();
emitter.emit('ping');
Aping
Bundefined
Cpong
DError: event not handled
Attempts:
2 left
💡 Hint

Think about what happens when the 'ping' event is emitted and how the listener responds.

📝 Syntax
intermediate
2:00remaining
Which option correctly adds a listener to a custom event emitter?

Given a custom event emitter instance, which code snippet correctly adds a listener for the 'data' event?

Node.js
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
Aemitter.on('data', () => console.log('received'));
Bemitter.addListener('data', console.log('received'));
Cemitter.listen('data', () => console.log('received'));
Demitter.on('data', console.log('received'));
Attempts:
2 left
💡 Hint

Remember the standard method to add event listeners in Node.js EventEmitter.

🔧 Debug
advanced
2:00remaining
Why does this custom event emitter code throw an error?

Examine the code below. Why does it throw an error when emitting the 'start' event?

Node.js
import { EventEmitter } from 'node:events';

class MyEmitter extends EventEmitter {
  constructor() {
    super();
  }
}

const emitter = new MyEmitter();
emitter.emit('start');
AThe constructor is missing a call to super(), causing an error.
BNo listener is registered for 'start', so emit returns false but no error is thrown.
CThe event name 'start' is invalid and causes an error.
Demit throws an error if no listeners exist for the event.
Attempts:
2 left
💡 Hint

Think about what happens when an event is emitted but no listeners are attached.

state_output
advanced
2:00remaining
What is the value of count after emitting events?

What is the value of count after the code runs?

Node.js
import { EventEmitter } from 'node:events';

class Counter extends EventEmitter {
  constructor() {
    super();
    this.count = 0;
    this.on('increment', () => {
      this.count += 1;
    });
  }
}

const counter = new Counter();
counter.emit('increment');
counter.emit('increment');
counter.emit('increment');

const count = counter.count;
A3
B0
Cundefined
DError: count is not defined
Attempts:
2 left
💡 Hint

Each 'increment' event increases count by 1. How many times is it emitted?

🧠 Conceptual
expert
2:00remaining
Which option best explains why custom event emitters improve Node.js apps?

Why do developers use custom event emitter classes in Node.js applications?

AThey enforce strict type checking on event names and listener arguments.
BThey automatically optimize CPU usage by managing event loops internally.
CThey replace the need for callbacks and promises entirely in asynchronous code.
DThey allow decoupling of code by letting parts communicate via events without direct calls.
Attempts:
2 left
💡 Hint

Think about how events help different parts of an app work together without tight connections.