Wednesday, 16 September 2020

Poll a service until I get a response or time out after certain time period

Requirement call a service.

If that service returns data, set data to a variable. functionality over

If that service returns data = null, call that service every 20 sec on repeat until it returns data= "a list or an object" or call the service for 2 mins and stop.

What I tried Need to Poll this service getUrlById(id) every 20 second, until I get the response in this.url or time out after 6 minutes.

Tried the below solution which is not working as expected.

pollUrl(id) {
    interval(2000).pipe(
      timeout(600000),
      takeWhile(() => this.url)
    ).subscribe(i => {
      this.service.getUrlById(id).subscribe(res =>{
        if(res && res["result"]){
          this.url = res["result"];
        }
      })
      })
  }

From Comments What I tried

Invoked the dummy service here, demo.

Here dummy service returns data = null. So according to requirement I need to call the service every 20 second till 2 minutes. That is not working.

Its not necessary to use this code, I want achieve the requirement. Can have different approach.



from Poll a service until I get a response or time out after certain time period

No comments:

Post a Comment