Retrying Http Requests With RxJs
- In this post I will show how to retry http requests using RxJs.
- Most of the time you don’t need retry logic for http request, but when you do, RxJs makes this very easy.
- Essentially all you have to do is tack on a retry(n) call to the http operation as seen below: – – Here the http request is retried up to five times in case of failure.
- The retryWhen operator is actually very flexible since it allows you to manage the retry pipeline as an observable, with all the power the RxJs toolbox has to offer.
- take(5) ‘Sorry, there was an error (after 5 retries)’})); }); – – From retryWhen I am returning a new observable with lot’s of bells and whistles.
In this post I will show how to retry http requests using RxJs.
@ngdocio: New blog “Retrying Http Requests With RxJs” has been published on @angular’s Unofficial Docs!
My first introduction to RxJs was through Angular’s http implementation. I was initially very skeptical of the move to RxJs as a core piece of the http module.
In fact I raised a Github issue on this topic very early on.
Since then I have moved on to agreement, with a touch of doubt, when it comes to the decision to move to RxJs.
It’s not that I am a doubter of RxJs as a library. It’s more that I think the benefits are sometimes exaggerated when compared to promise based counterparts – especially in the context of http.
Not to mention the overhead involved in training people to successfully use RxJs.
This however is a gift to people who offer training courses since it definitely increases the demand for their material. I guess I should be grateful for this since some of my most visited articles are about RxJs 🙂
The way I see it, there are two major selling points for RxJs based http.
The first, and perhaps the most important benefit, is the ability to cancel a request in “flight”. I talk a little bit about that in one of my other articles.
Of course when I say “cancel”, I am not necessarily talking about preventing the request from hitting the server. “Cancellation” generally means preventing processing of the http response on the…