retryWhen
signature: retryWhen(receives: (errors: Observable) => Observable, the: scheduler): Observable
retryWhen(receives: (errors: Observable) => Observable, the: scheduler): ObservableRetry an observable sequence on error based on custom criteria.
Examples
Example 1: Trigger retry after specified duration
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { timer, interval } from 'rxjs';
import { map, tap, retryWhen, delayWhen } from 'rxjs/operators';
//emit value every 1s
const source = interval(1000);
const example = source.pipe(
map(val => {
if (val > 5) {
//error will be picked up by retryWhen
throw val;
}
return val;
}),
retryWhen(errors =>
errors.pipe(
//log error message
tap(val => console.log(`Value ${val} was too high!`)),
//restart in 6 seconds
delayWhen(val => timer(val * 1000))
)
)
);
/*
output:
0
1
2
3
4
5
"Value 6 was too high!"
--Wait 6 seconds then repeat
*/
const subscribe = example.subscribe(val => console.log(val));Example 2: Customizable retry with increased duration
( StackBlitz )
Additional Resources
retryWhen 📰 - Official docs
retryWhen - In Depth Dev Reference
Error handling operator: retry and retryWhen 🎥 💵 - André Staltz
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/retryWhen.ts
Last updated