0

I have a scenario where I need to call a service method(which will call HTTP post method) repetitively until I get the results and for that I am using interval. In below code the getReportResultsList(id) method will be get called in the interval of 100 ms.

The problem is, the server may take 300 ms time to process the first request and return the results. But at the same time 2 more requests are going to the server to get the results because of interval(100). So after getting the results from the first request i don't want to process the unexpected 2nd and 3rd requests response results.

So I don't want to process the 2nd and 3rd responses. Can any one know how to to deal with these out of order responses?

Thanks.

let subscriber = Observable.interval(100).subscribe(() => {
                   this.reportService.getReportResultList(id)
                   .subscribe(result => {
                     this.reportResults = result;
                     if (this.reportResults) {
                       this.prepareViewData(result);
                       subscriber.unsubscribe();
                     }
                   });
                 });
4

1 回答 1

0

正如上面提到的评论者,您不需要为您的用例每秒调用服务器 10 次。您只需要调用服务器,直到您真正得到结果。

更适合使用retryWhenObservables 的语义。

这些方面的东西:

this.reportService.getReportResultList(id)
       .subscribe(result => {
         this.reportResults = result;
         if (!this.reportResults) {
           throw 'empty'; 
         }
         this.prepareViewData(result);
       })
       .retryWhen((errors) => return errors.delay(10));

您需要在 retryWhen 处理程序中更加彻底,因为可能会引发其他错误(例如:HTTP 50x 错误),但这会给您一个想法。

于 2016-12-22T14:01:32.263 回答