ReTry, ReTryWhen in Angular Observable

ReTry & ReTryWhen Operators help us to retry a failed observable in Angular. These operators are useful in Error handling. They both resubscribe to the source observable when they receive onError() notification.


The ReTry Angule RxJs operator retries a failed source observable count number of times. If the count is not provided then it tries indefinitely


Mirrors the source observable, if there are no errors.

If the source observable calls error notification, it does not propagate it. But re subscribes to the source again for a maximum of count times.

The Retry operator retries the operation immediately

ReTry Example

In the example below, map operator throws an error if the source emits a value greater than 3.

The ReTry(2) retries the operation twice before erroring out.

Source Code

Retry without any argument, will retry indefinitely

Retry(0) never retries.


The RetryWhen Angule RxJs operator retries the failed Observable every time a Notification Observable emits the next value.


Where notifier is the callback, which returns the Notification Observable

How it works

We register the notifier callback with the ReTryWhen Operator.

The notifier gets the errors observable as the argument, which emits whenever the source observable errors

We return the notifier observable, which we build using the errors observable.

The ReTryWhen subscribes to this notifier observable and how it behaves depends on the value emitted by the notifier observable

  • If the notifier emits a value, then ReTryWhen re subscribes the source observable.
  • In case of notifier emitting an error, then ReTryWhen also emits an error.
  • If the notifier completes, then ReTryWhen does nothing.

ReTryWhen Example

In the following example, map operator throws an error if the val > 2.

The errors are caught by retryWhen. It gets the error observable as its input. We use the pipe operator to add a tap to print the Retrying message on the console.

This code runs indefinitely as the source will always errors out and there is nothing to stop retryWhen

Source Code

Returning the error observable as it is also retries the source indefinitely

Source Code

Adding a Delay

The following code adds delay of 2000 ms.

Source Code

Notifier observable

As long as the notifier observable emits a value, the retryWhen will re subscribes to the source again.

In the following example, we switch observable to a new observable using SwitchMap operator. The new observable (of Operator) emits the value 1 after a delay of 1000 ms and the source is resubscribed again.

Source Code

The following uses of() instead of(1). of() does not emit any values but sends a complete notification on subscription. In this case, RetryWhen does not retry the source but completes. The subscribers will not receive any notification.

Source Code

Here is another interesting example where we switch to interval(3000) observable.

When the error occurs the first time in the source, RetryWhen triggers the interval(3000). When it emits a value source is re-subscribed.

But, the interval(3000) is going to emit another value after 3000ms. In this case, the RetryWhen will re subscribes to the source again, even if the source has errored out or already completed.

Source Code

Limiting Retry Attempts

In the example below, we use the scan operator to count the number of tries and throw an error if the number of attempts exceeds 2.

Source Code

Use the dealyWhen to increase the time duration between each retries.

Source Code

You can also use the take or takeWhile operator to stop the retries and emit the complete notification.


Read More

1 thought on “ReTry, ReTryWhen in Angular Observable”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top