An async provider uses useFactory with an async factory function. NestJS awaits the factory before the application starts — ensuring the provider is fully initialized before any consumer receives it. This solves the problem of providers requiring async initialization like database connections or remote config loading.
Some providers need to perform asynchronous operations during initialization — for example, establishing a database connection or fetching configuration from a remote service. NestJS handles this by awaiting the factory function before completing the bootstrap process.
NestJS awaits the async factory before allowing any provider that depends on it to be resolved.
The resolved value (e.g., the Connection object) is what gets injected into consumers.
Async providers can inject other synchronous or async providers via the inject array.
If the factory throws, the entire application bootstrap fails — handle errors appropriately.