Challenge - 5 Problems
NestJS Provider Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a provider is set to default scope in NestJS?
Consider a NestJS service provider with the default scope. What is the behavior of this provider when injected into multiple components?
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class MyService { id = Math.random(); } // This service is injected into two different controllers or providers.
Attempts:
2 left
💡 Hint
Default scope means the provider is a singleton by default.
✗ Incorrect
In NestJS, providers with default scope are singletons. This means only one instance is created and shared across the whole app.
❓ component_behavior
intermediate2:00remaining
How does a request-scoped provider behave in NestJS?
If a provider is set with request scope, what happens when multiple HTTP requests use this provider?
NestJS
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.REQUEST }) export class RequestService { id = Math.random(); } // This service is injected into controllers handling HTTP requests.
Attempts:
2 left
💡 Hint
Request scope means one instance per HTTP request.
✗ Incorrect
Request-scoped providers create a new instance for each incoming HTTP request, ensuring isolated state per request.
❓ component_behavior
advanced2:00remaining
What is the behavior of a transient-scoped provider in NestJS?
A provider is marked with transient scope. What happens when it is injected multiple times in the same request?
NestJS
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.TRANSIENT }) export class TransientService { id = Math.random(); } // Injected multiple times in the same request or component.
Attempts:
2 left
💡 Hint
Transient scope means no instance sharing at all.
✗ Incorrect
Transient providers create a new instance every time they are injected, even within the same request or component.
❓ state_output
advanced2:30remaining
What will be the output of the following NestJS provider instances' IDs?
Given these providers with different scopes, what will be the output of their id properties when injected twice in the same request?
NestJS
import { Injectable, Scope } from '@nestjs/common'; @Injectable() export class DefaultService { id = Math.random(); } @Injectable({ scope: Scope.REQUEST }) export class RequestService { id = Math.random(); } @Injectable({ scope: Scope.TRANSIENT }) export class TransientService { id = Math.random(); } // Assume these are injected twice in the same request and their ids logged.
Attempts:
2 left
💡 Hint
Think about how many instances each scope creates per injection or request.
✗ Incorrect
Default scope creates one instance shared everywhere, so ids are same. Request scope creates one instance per request, so ids same within request. Transient creates new instance every injection, so ids differ.
❓ lifecycle
expert2:30remaining
Which provider scope causes the most frequent constructor calls in NestJS?
Consider a provider injected multiple times in various parts of an app. Which scope leads to the highest number of constructor executions?
Attempts:
2 left
💡 Hint
Think about how often new instances are created for each scope.
✗ Incorrect
Transient scope creates a new instance every injection, causing the constructor to run most frequently.