On this page
retry
function
stable
operator
Returns an Observable that mirrors the source Observable with the exception of an error
.
retry<T>(configOrCount: number | RetryConfig = Infinity): MonoTypeOperatorFunction<T>
Parameters
configOrCount |
number | RetryConfig |
Optional. Default is Either number of retry attempts before failing or a RetryConfig object. |
Returns
MonoTypeOperatorFunction<T>
: A function that returns an Observable that will resubscribe to the source stream when the source stream errors, at most count
times.
Description
If the source Observable calls error
, this method will resubscribe to the source Observable for a maximum of count
resubscriptions rather than propagating the error
call.
The number of retries is determined by the count
parameter. It can be set either by passing a number to retry
function or by setting count
property when retry
is configured using RetryConfig. If count
is omitted, retry
will try to resubscribe on errors infinite number of times.
Any and all items emitted by the source Observable will be emitted by the resulting Observable, even those emitted during failed subscriptions. For example, if an Observable fails at first but emits [1, 2]
then succeeds the second time and emits: [1, 2, 3, 4, 5, complete]
then the complete stream of emissions and notifications would be: [1, 2, 1, 2, 3, 4, 5, complete]
.
Example
import { interval, mergeMap, throwError, of, retry } from 'rxjs';
const source = interval(1000);
const result = source.pipe(
mergeMap(val => val > 5 ? throwError(() => 'Error!') : of(val)),
retry(2) // retry 2 times on error
);
result.subscribe({
next: value => console.log(value),
error: err => console.log(`${ err }: Retried 2 times then quit!`)
});
// Output:
// 0..1..2..3..4..5..
// 0..1..2..3..4..5..
// 0..1..2..3..4..5..
// 'Error!: Retried 2 times then quit!'
See Also
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/retry