RxJS retryWhen() Error Handling Operator
RxJS retryWhen() operator is an error-handling operator used to return an observable that mirrors the source observable except an error. If the source observable calls an error, this method will emit the Throwable that caused the error to the observable returned from the notifier. If that observable call is completed or got an error, this method will call complete or error on the child’s subscription. Otherwise, this method will resubscribe to the source observable.
In other words, we can say that the RxJS retryWhen() operator enables to retry the request instead of giving up after the first failure. It facilitates you to retry an observable sequence of errors based on custom criteria.
The function provided to the operator receives a stream of errors, and we need to return an observable, which will trigger the requested retry after every value emission.
Syntax:
Following is the syntax of the RxJS retryWhen() error handling operator:
Or
Parameter Explanation
- notifier: It receives an observable of notifications which facilitates the user to complete or getting error, aborting the retry.
Return value
The RxJS retryWhen() operator returns an observable that retry count logic and is modified with the retry logic.
Let us see some examples of the RxJS retryWhen() operator to understand it clearly.
Example 1 (Trigger retry after specified duration)
Output:
After executing the above example, you will see the following result:
In the above example, you can see that it has emitted the specified values only and then wait for 6 seconds and then repeat.
Example 2 (Customizable retry with increased duration)
Output:
After executing the above example, you will see the following result: