Asynchronous Providers
때떄로, 한 두개의 asynchronous task가 완료된 이후 application을 실행할 필요가 있다. 예를 들어 DB와의 연결이 완료될 확릴될 때까지 요청을 수락하지 않도록 하고 싶을 수 있다. asynchronous provider를 통해 이를 달성할 수 있다.
이를 위한 문법은 useFacotry에서의 async/await이다. factory는 Promise를 return한다. 그리고 await asynchronous task를 수행한다. Nest는 그러한 provider에 의존하는(주입받는) 클래스를 instance화하기 전에 promise가 해결될 때 까지 기다릴 것 이다.
{
provide: 'ASYNC_CONNECTION',
useFactory: async () => {
const connection = await createConnection(options);
return connection;
},
}
<팁>
custom provider syntax에 대해 자세히
Injection
Asynchronous provider는 다른 provider 처럼 token에 의해 다른 component에 주입된다. 예를 들어, 위의 코드는 @Inject('ASYNC_CONNECTION')을 사용한다.
Example
The TypeORM Recipe에 asynchronous provider의 상세한 예가 있다.
'NestJS > Docs_FUNDAMENTALS' 카테고리의 다른 글
Custom Providers (0) | 2024.06.17 |
---|