Performance: Async configuration
Async configuration affects the app startup time and initial rendering by delaying module initialization until async tasks complete.
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ ConfigModule.forRootAsync({ useFactory: async () => { const config = await fetchRemoteConfig(); return config; }, inject: [], }), ], }) export class AppModule {}
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot()], }) export class AppModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous config loading | Minimal DOM nodes initially | 0 reflows from config but blocks JS execution | Delays first paint due to blocking | [X] Bad |
| Async config with useFactory | Minimal DOM nodes initially | 0 reflows, non-blocking JS execution | Faster first paint by deferring config load | [OK] Good |
forRootAsync in NestJS configuration?forRootAsync is designed to load configuration that requires asynchronous operations like fetching from a remote service.forRootAsync with a factory function in NestJS?forRootAsync and the correct property for async config is useFactory.forRootAsync with useFactory returning an object asynchronously, which is correct. ConfigModule.forRoot({ useFactory: () => ({ key: 'value' }) }) uses forRoot which is synchronous. ConfigModule.forRootAsync({ factory: () => ({ key: 'value' }) }) uses wrong property factory. ConfigModule.forRootAsync({ useFactory: () => 'value' }) returns a string instead of an object.ConfigModule.forRootAsync({
useFactory: async () => {
const apiKey = await Promise.resolve('12345');
return { apiKey };
}
})ConfigModule.forRootAsync({
useFactory: () => {
const secret = fetchSecret();
return { secret };
}
})
async function fetchSecret() {
return 'topsecret';
}forRootAsync?useFactory, it must be injected via the inject array and passed as a parameter.useFactory async, injects DatabaseService, and awaits getConfig(). Options B and C miss injection or async. ConfigModule.forRoot({ useFactory: async (dbService) => await dbService.getConfig(), inject: [DatabaseService] }) uses forRoot which is synchronous.Pass the test and this pattern moves unknown