Search code examples
javascriptspfx

How to call a function after promise


JavaScript noobie here. Lacking knowledge of an important design pattern.

this.getEventData(); < How can I call that function after the promise is resolved? So then I can remove the noobie setTimeout ?

  public onInit(): Promise<void> {
    return super.onInit().then(_ => {
      new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
        this.context.aadHttpClientFactory
          .getClient(APIAppRegID)
          .then((client: AadHttpClient): void => {
            this.httpClient = client;
            resolve();
          }, err => reject(err));
      });
      setTimeout(() => {
        this.getEventData();
      }, 1500);
    });
  }

Solution

  • async/await version :

    async onInit(): Promise<void> {
      await super.onInit();
      this.httpClient = await this.context.aadHttpClientFactory.getClient(APIAppRegID);
      this.getEventData();
    }