Challenge - 5 Problems
Event Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when an event is emitted?
Consider a NestJS event emitter setup where a listener logs a message when an event named 'user.created' is emitted. What will be logged when the event is emitted with payload { id: 42 }?
NestJS
import { Injectable } from '@nestjs/common'; import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; @Injectable() export class UserService { constructor(private eventEmitter: EventEmitter2) {} createUser() { this.eventEmitter.emit('user.created', { id: 42 }); } } @Injectable() export class UserListener { @OnEvent('user.created') handleUserCreatedEvent(payload: { id: number }) { console.log(`User created with id: ${payload.id}`); } }
Attempts:
2 left
💡 Hint
Think about how the @OnEvent decorator works and what payload is passed to the listener.
✗ Incorrect
The @OnEvent decorator listens for the 'user.created' event. When the event is emitted with payload { id: 42 }, the listener logs the message with the id value.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an event listener in NestJS?
You want to listen to an event named 'order.completed' in a NestJS service. Which code snippet correctly sets up the listener?
Attempts:
2 left
💡 Hint
Check the official NestJS event emitter decorator name.
✗ Incorrect
The correct decorator to listen to events in NestJS is @OnEvent. Other decorators are invalid and will cause errors.
🔧 Debug
advanced2:30remaining
Why does the event listener not respond?
Given the following NestJS code, why does the listener not log anything when the event is emitted?
NestJS
import { Injectable } from '@nestjs/common'; import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; @Injectable() export class NotificationService { constructor(private eventEmitter: EventEmitter2) {} sendNotification() { this.eventEmitter.emit('notification.sent', { message: 'Hello' }); } } @Injectable() export class NotificationListener { @OnEvent('notification.send') handleNotification(payload: { message: string }) { console.log('Notification:', payload.message); } }
Attempts:
2 left
💡 Hint
Check the event names carefully for exact matches.
✗ Incorrect
Event names must match exactly. Here, the listener listens for 'notification.send' but the event emitted is 'notification.sent', so the listener never triggers.
❓ state_output
advanced2:30remaining
What is the final state after emitting multiple events?
A NestJS service emits two events in sequence. A listener updates a count each time it receives the event. What is the final count after both events?
NestJS
import { Injectable } from '@nestjs/common'; import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; @Injectable() export class CounterService { private count = 0; constructor(private eventEmitter: EventEmitter2) {} increment() { this.eventEmitter.emit('counter.increment'); } getCount() { return this.count; } } @Injectable() export class CounterListener { private count = 0; @OnEvent('counter.increment') handleIncrement() { this.count += 1; } getCount() { return this.count; } } // In application code: // counterService.increment(); // counterService.increment(); // What is counterListener.getCount()?
Attempts:
2 left
💡 Hint
Events decouple the emitter and listener. The listener updates its own state independently each time the event is emitted.
✗ Incorrect
The CounterService emits the 'counter.increment' event twice. The CounterListener receives both events via the @OnEvent decorator and increments its private count each time. Therefore, counterListener.getCount() returns 2. Note that the service has its own unused count which remains 0.
🧠 Conceptual
expert3:00remaining
Which statement best describes event propagation in NestJS event emitter?
In NestJS using EventEmitter2, what happens when multiple listeners are registered for the same event and one listener throws an error during event handling?
Attempts:
2 left
💡 Hint
Think about synchronous event emission and error handling in EventEmitter2 by default.
✗ Incorrect
By default, EventEmitter2 calls listeners synchronously in order. If a listener throws an error, the error propagates and stops further listeners from running unless caught.