0
0
NestJSframework~20 mins

Async configuration in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Config Master
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 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 {}
AError: useFactory cannot be async
BConfig loaded\nLoading config...
CNo output because useFactory is async
DLoading config...\nConfig loaded
Attempts:
2 left
💡 Hint
Think about how async functions execute and when console.log statements run.
state_output
intermediate
2: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 {}
A{ port: 3000 }
BPromise { <pending> }
Cundefined
DError: Cannot inject async provider
Attempts:
2 left
💡 Hint
Remember NestJS waits for async providers to resolve before injection.
📝 Syntax
advanced
2: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 {}
AuseFactory: async () => ({ envFilePath: '.env' })
BuseFactory: () => { envFilePath: '.env' }
CuseFactory: async () => { return { envFilePath: '.env' } }
DuseFactory: () => Promise.resolve({ envFilePath: '.env' })
Attempts:
2 left
💡 Hint
Check which option returns the config object correctly from an async function.
🔧 Debug
advanced
2: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' };
}
AConfig is undefined
BSyntaxError: 'await' used outside async function
CfetchConfig is not defined
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check if the function using await is declared async.
🧠 Conceptual
expert
3: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?
ANestJS throws a runtime error about circular dependency
BBoth providers resolve successfully without issues
COne provider resolves, the other remains pending forever
DNestJS automatically breaks the cycle and injects partial values
Attempts:
2 left
💡 Hint
Think about how dependency injection handles circular references.