0

Is it possible in Angular to retry a connection when the calls stays for more then a couple of seconds on pending?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hans
  • 287
  • 1
  • 5
  • 18

1 Answers1

5

Should be doable with a combination of pipe, timeout and retry from Rxjs. If timeout is exceeded, retry 4 times, else, catchError.

return this.httpClient.post(url, data, httpOptions).pipe(
  timeout(3000),
  retry(4),
  catchError(<DO SOMETHING>)
);
prettyfly
  • 2,962
  • 1
  • 25
  • 27
  • what does the .pipe do and where can i put the .map with the response now? – Hans Jun 04 '18 at 11:55
  • @Hans - `.pipe()` combines the the operators into an observable stream. Using Angulars `HttpClient`, the methods already return an observable of the response, so you can do any manipulation in your observer. e.g `this.httpClient.post(url, data, httpOptions).pipe( timeout(3000), retry(4), catchError(this.handleError)).subscribe((result: any) => { // do your data manipulation here..... });` – prettyfly Jun 04 '18 at 13:49
  • As an aside, you can encapsulate your http methods in a service (e.g `post`, `get`, etc), that way you can set defaults for timeouts and retries etc, then just subscribe to a service method (for example... `doPost`) in your other components. More here: https://angular.io/guide/http#getting-json-data – prettyfly Jun 04 '18 at 13:51