简体   繁体   中英

How to handle async operation in angular?

I am trying to initiate a modal component on ngOnInit() by passing fragments in route link

  ngOnInit() {
    this.getCandidateDetails()

    this.route.fragment.subscribe(qp => {
      if ('openjobeditmodal' == qp) {
        this.editJobRole();
      }
    })
    
  }

now I am able to open a modal component on page initiation but the issue is that component takes this.getCandidateDetails() response as Input,and this.getCandidateDetails() is getting called after modal initiate,how can I ensure thatI only subscribe to modal only after this.getCandidateDetails() gets called

Make this.getCandidateDetails() return a Promise, then await the Promise completion before subscribing the observable:

async ngOnInit() {
  await this.getCandidateDetails()

  this.route.fragment.subscribe(qp => {
    if ('openjobeditmodal' == qp) {
      this.editJobRole();
    }
  })
}

Share the code of your getCandidateDetails() if you want help at making it return a promise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM