1

We use a HTTP client in Angular 6 to get a page of data. When multiple requests sent out too quick, for example, user entering filter characters, the page requests keep sending out quickly. the results back is out of order easily. Suppose the HTTP client called three times, the final results coming back may be is for 1st service request.

How to solve this issue? Thanks in advance.

Hans
  • 123
  • 7
  • 1
    Have you tried rxjs denounce? – isAif May 21 '20 at 01:55
  • Send a CancelationRequestToken to the server before sending your next request. – Zze May 21 '20 at 01:56
  • It's not a http issue. Rather it's how the requests are sent. All requests are asynchronous which means there's no order guarantee for simultaneous requests. Use RxJS interval or debounce to find perfect timing. Start with 200ms then reduce until problem happens again – JWP May 21 '20 at 02:13
  • https://www.learnrxjs.io/learn-rxjs/operators/filtering/debouncetime This would be a good place to start – Darren Street Jun 12 '20 at 17:21

1 Answers1

0

You can use switchMap for having a canceling effect. One similar question and answer is : here

If you are new to rxjs then here are top-level steps :

1) Create a service and create one request subject: request$ = new Subject();

2) Whenever you want to make hhtp request, just publish data to above subject.

3) In the same service class, create a method which will return observable :

getServiceData():Observable { return request$.pipe(switchMap(() => this.callActualService()); }

4) Subscribe to this Observable from Controller.

Pratik soni
  • 88
  • 1
  • 3
  • 12