All quizzesMedium
Custom Providers
Preview — 3 of 10 questions
What does the useValue provider syntax accomplish?
javascript
const config = { timeout: 5000 };
@Module({
providers: [{ provide: 'HTTP_CONFIG', useValue: config }],
})
export class HttpModule {}AIt instantiates a class lazily
BIt creates a new instance per request
CIt runs a factory function on every injection
DIt registers an existing value/object/constant under a token so it can be injected directly
How can you make the token LoggerService resolve to FileLogger in production but ConsoleLogger elsewhere?
javascript
@Module({
providers: [
{
provide: LoggerService,
useClass: process.env.NODE_ENV === 'production' ? FileLogger : ConsoleLogger,
},
],
})
export class LoggingModule {}ARegister { provide: LoggerService, useClass: isProd ? FileLogger : ConsoleLogger }
BUse two @Injectable() decorators
CSubclass both at runtime
DIt is impossible to swap implementations
When using useFactory, how do you supply dependencies to the factory function?
javascript
@Module({
providers: [
{
provide: 'DB_CLIENT',
useFactory: (config: ConfigService) => new DbClient(config.get('DB_URL')),
inject: [ConfigService],
},
],
})
export class DbModule {}AImport them as globals inside the factory
BFactories cannot have dependencies
CList their tokens in the inject array; Nest resolves them and passes them as factory arguments in order
DUse @Inject() inside the factory body
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.