[NestJS 공식문서 정독하기] Fundamentals - Asynchronous providers

2022. 8. 7. 18:05Web/NestJS

반응형
🔗  https://docs.nestjs.com/fundamentals/async-providers
 

Documentation | NestJS - A progressive Node.js framework

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reac

docs.nestjs.com

{
  provide: 'ASYNC_CONNECTION',
  useFactory: async () => {
    const connection = await createConnection(options);
    return connection;
  },
}
  • 가끔 애플리케이션 시작이 하나 혹은 여러개의 비동기 처리가 끝날때까지 지연되야 하는 경우가 있음 (에를 들어 DB와의 커넥션이 연결되기 전에 요청을 받기 시작하면 안됨)
  • asynchronous providers를 사용하면 이러한 부분을 해결할 수 있음
  • useFactory 에서 async/await 을 사용해서 처리할 수 있음 (factory 함수는 Promise를 반환하고, 해당 provider를 의존하는 클래스를 인스턴스화 하는 것을 지연시킴)
  • TypeORM recipe가 더욱 실질적인 예시를 담고 있음
반응형