Challenge - 5 Problems
Async Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this async configuration in NestJS?
Consider this NestJS module configuration using async factory. What will be logged when the application starts?
NestJS
import { Module } from '@nestjs/common'; @Module({ imports: [], providers: [ { provide: 'CONFIG', useFactory: async () => { console.log('Loading config...'); await new Promise(resolve => setTimeout(resolve, 100)); console.log('Config loaded'); return { key: 'value' }; }, }, ], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Think about how async functions execute and when console.log statements run.
✗ Incorrect
The async useFactory runs immediately when the provider is created. The first console.log runs before the await, so 'Loading config...' prints first. After the await delay, 'Config loaded' prints. So the output is those two lines in order.
❓ state_output
intermediate2:00remaining
What value does the injected config have after async initialization?
Given this async config provider in NestJS, what is the value injected into dependent services?
NestJS
import { Module, Inject, Injectable } from '@nestjs/common'; @Injectable() class Service { constructor(@Inject('CONFIG') public config: any) {} } @Module({ providers: [ { provide: 'CONFIG', useFactory: async () => { return new Promise(resolve => { setTimeout(() => resolve({ port: 3000 }), 50); }); }, }, Service, ], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Remember NestJS waits for async providers to resolve before injection.
✗ Incorrect
NestJS resolves async providers before injecting them. So the injected value is the resolved object { port: 3000 }, not a Promise or undefined.
📝 Syntax
advanced2:00remaining
Which option correctly uses async configuration with ConfigModule.forRoot?
Select the correct async configuration syntax for ConfigModule in NestJS.
NestJS
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ ConfigModule.forRoot({ // async config here }), ], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Check which option returns the config object correctly from an async function.
✗ Incorrect
Option C correctly defines an async function returning the config object with explicit return. Option C misses return statement (implicit return with parentheses is valid but here it's an object literal, so parentheses needed). Option C returns a Promise but not async function syntax. Option C returns undefined because no return statement.
🔧 Debug
advanced2:00remaining
Why does this async config provider cause a runtime error?
Identify the cause of the runtime error in this async provider code.
NestJS
providers: [
{
provide: 'CONFIG',
useFactory: () => {
const config = await fetchConfig();
return config;
},
},
],
async function fetchConfig() {
return { key: 'value' };
}Attempts:
2 left
💡 Hint
Check if the function using await is declared async.
✗ Incorrect
The useFactory function is not marked async but uses await inside, causing a SyntaxError.
🧠 Conceptual
expert3:00remaining
What happens if multiple async config providers depend on each other circularly?
In NestJS, if two async providers depend on each other (circular dependency) during async configuration, what is the expected behavior?
Attempts:
2 left
💡 Hint
Think about how dependency injection handles circular references.
✗ Incorrect
NestJS detects circular dependencies and throws a runtime error to prevent infinite loops during provider resolution.