Tutorial hero
Lesson icon

RxJS retry Operator

Originally published July 13, 2023 Time 2 mins

RxJS has a super convenient retry operator, which is incredibly useful for situations where a request for data fails.

For example, you are trying to load some data in from an API and it errors. The retry operator, rather than letting the stream itself error, will just try again under whatever restrictions you propose.

For example, you might want it to retry 5 times and then give up:

retry 5 times

Another useful operator that I occasionally have had to use is retryWhen - which is a little more awkward to use, but still incredibly useful for situations where you need more control over when something is retried.

But! retryWhen has actually been deprecated now in favour of using the same retry operator in conjunction with a delay function:

retry operator in conjunction with a delay function

This allows you to supply a notifier observable, such that whenever that observable emits the retry will be triggered at that point.

This is perfect for a situation where you want some kind of user initiated retry or perhaps restarting a stream of data once a user has re-authenticated (I’ve done this before to restart a stream of data from Firebase when a user logs back in).

Learn to build modern Angular apps with my course