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();
                     }
                   });
                 });
Yashavanta SB
  • 79
  • 3
  • 11
  • I believe you don't need to use Interval to do this kind of thing. If you have several http calls to make and you want to reduce it, you could check this link : http://stackoverflow.com/questions/39395290/update-several-component-properties-with-a-single-http-call-using-observables Using obervables should be the solution since you only need to wait for your call to be done to get the results. – Alex Beugnet Dec 21 '16 at 08:38
  • @AlexBeugnet can you suggest me how to make repetitive call until i get the results. Thanks. – Yashavanta SB Dec 21 '16 at 09:36
  • I would need more information on what kind of result you are expecting. This is still unclear to me : `The problem is, the server may take 300 ms time to process the first request and return the results. But at the same time after 100 ms it will make one more request to get the results and so on.` The link I showed you earlier was a solution when you have several components that make HTTP calls so that you can actually make only one HTTP call to get the data for all these components. I might have misunderstood and you might need something else... – Alex Beugnet Dec 21 '16 at 09:40
  • I have a component which displays some report results. In order to get the results i am making calls to the server. When i make a first call, the results may not prepared yet(we are using some tasks to prepare the results). So again i have to make one more call. Even at the second request also results may not be prepared yet.... So i have to make repetitive calls to get the results. That's why i am using observable interval method. – Yashavanta SB Dec 21 '16 at 09:59
  • With what you just said, the only thing that comes to my mind is to make it so that on your first call you know if the results are there or not, and if not, redo the call only when you know the data is there. That would limit the number of calls to two. That means from your server, you have to find a way to know when the results will be there. I don't know how your tasks are made on the server, but it would be something like : `first http call => return the time when the results will be there => make the second call after that time => get results`. This seems more like a server issue – Alex Beugnet Dec 21 '16 at 10:25

1 Answers1

0

As the commenters mentioned above, you don't need to call the server 10 times per second for your use case. You only need to call the server until you actually have a result.

More appropriate to use the retryWhen semantics of Observables.

Something along these lines:

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

You need to be a little more thorough in the retryWhen handler because other errors could be raised (example: an HTTP 50x error) but that'll give you an idea.

David M.
  • 2,602
  • 1
  • 15
  • 15
  • You'll find everything you need to know about handling HTTP errors with Angular 2 here: https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling – David M. Dec 24 '16 at 13:32