async constructor?

succeeding·2022년 8월 26일

Class의 constructor에는 예외적으로 async를 붙일 수 없다. 이런 작업이 필요할 때 대안으로 다음의 방법을 사용하자

class A {
  constructor(){
    return this.init();
  }
  
  async init(){
    await someAsyncFunction();
    return this;
  }
}

출처

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=bitofsky&logNo=220942834796

0개의 댓글