24 Jul 2024
1 min

retry – RxJS Reference

retry

Just as a plain observable retry passes values along to the observer (mirroring) until an error in the source stream occurs. When that happens, the operator will resubscribe to the source observable instead of propagating the error to the observer. The number of times this resubscription will occur on an error is defined by the count parameter passed to the operator.

Please note, that the observer will receive all items emitted by the source observable, even those emitted before the error occurred. For example, if the source observable emits values {1,2}, then fails, then resubscription happens and the stream successfully emits values {1,2,3,4} before completing, the observer receives the following sequence of values {1,2,1,2,3,4}.

retry operator works in the following way:

  1. Subscribe to a source observable
  2. When a new value arrives from a source observable, send the value to the observer
  3. If the source observable throws an error, compare the number of defined retries with the number of occurred resubscriptions
  4. If the number of subscriptions is less, resubscribe, otherwise send the error notification to the observer
  5. Once the source observable completes, send the complete notification to the observer

Usage

Errors in programs happen all the time and RxJS is no exception. For example, a network request can fail because of numerous reasons. It’s always useful to have a mechanism to retry an action.

retry can be used to retry a failed network request. But when you need to retry based on some criteria, for example, an error description, you need to use retryWhen.

Here’s an example of using retry to retry the request twice in case of an error:

const url = 'https://i.imgur.com/fHyEMsl.jpg';

fromFetch(url).pipe(
   switchMap((response) => response.json()),
   // triggers 3 network requests:
   // 1 initial and 2 retries
   retry(2)
).subscribe();

Playground

Additional resources

See also

Frequently asked questions

How do I retry an RxJS observable when it errors?

Use the retry() operator in a pipe(), passing the number of resubscriptions as its count argument, for example retry(2). When the source throws, retry() resubscribes instead of forwarding the error, so retry(2) produces one initial request plus two retries, three attempts in total. Values emitted before a failure are still delivered to the observer on every attempt.

What is the difference between retry() and retryWhen() in RxJS?

retry() resubscribes a fixed number of times on any error, controlled by its count parameter. retryWhen() should be used when you need to decide whether to retry based on criteria such as the error description, because it lets you control the retry logic with a notifier observable. Choose retry() for simple, unconditional retries like a failed network request.

Does retry() emit values from the failed attempt again after resubscribing?

Yes. The observer receives every value the source emits, including values emitted before the error occurred. If the source emits 1 and 2, fails, and then succeeds emitting 1, 2, 3, 4, the observer receives 1, 2, 1, 2, 3, 4. Account for this duplication when the source emits side-effect-free state updates or accumulates results.

How do I retry a failed fetch request in RxJS with fromFetch?

Pipe the fromFetch() observable through switchMap() to parse the response, then add retry(2) after it. The retry(2) call triggers three network requests in total: one initial request and two retries. Place retry() after the operator that might throw, so a failed response.json() call also triggers a resubscription to the request.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.