Angular 4 RxJS retry failed request

How To handle and retry the failed request due to Poor Network Connection in Angular 4?

In Angular 4, simply retry the request using RxJS operator called .retry (). It is automatically presubscribed to an Observable, thus reissuing the request when an error occurred!

Import RxJS for using retry () method-
import 'rxjs/add/operator/retry';

HTTP Observables for retry the failed request 
http.get('/api/users')
  .retry(2) //Retry this request up to 2 times.
  .subscribe(data => {console.log(data);}, //Successful responses call the first callback.
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
           console.log('Error - ', err.error.message);
        }else {
            console.log('Error status - ${err.status}, and Error Detail - ${err.error}');
        }
      }
});


References -
I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

How To handle and retry the failed request due to Poor Network Connection in Angular 4? How To handle and retry the failed request due to Poor Network Connection in Angular 4? Reviewed by Anil Singh on 4:44 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^