1

I'm working on creating API polling on some async function which is called from another async function. Something like below code:

public generatePreview() {
this.apiService.firstAPI().subscribe(async headers => {
  // Do some processing over here
  await this.getRows();
});
}


  public async getRows() {
    this.apiService.secondAPI().subscribe(async rows => {
      if (rows.status === 'INGESTION_IN_PROGRESS') {
        console.log('inside');
        setTimeout(()=>{return await this.getRows()}, 5000);
      } else if (rows.status === 'COMPLETED') {
          // Stopping Logic
      }
    });
  }

Explanation : generatePreview function calls a firstAPi function, calls out getRows function which has the polling logic . If async call response has status 'INGESTION_IN_PROGRESS', It should call getRows again(with some delay). When I put it inside setTimeout, I get the following error ''await' expression is only allowed within an async function.'.

I am looking for best options to achieve the polling with some timer.

Riya
  • 794
  • 2
  • 9
  • 17
  • maybe use Promise constructor to construct a new promise which waits and resolves after delay has passed. Else you may try to async/await in setTimeout but instead of returning value simply set a global var with the result and check if it is set later on, but 1st option is better – Nikos M. Mar 17 '19 at 09:04

0 Answers0