How to get catch after retryWhen?
The problem here is that when the notification Observable returned from the callback in retryWhen
sends the complete
notification it's propagated further as complete
which is not what you want from your description.
You want to send it as error
notification which means you can't use take()
and use some other operator to rethrow the error.
For example like this:
Observable.defer(() => Observable.throw("It's broken"))
.retryWhen(err => {
console.log('retry');
let retries = 0;
return err
.delay(1000)
.map(error => {
if (retries++ === 5) {
throw error;
}
return error;
});
})
.catch(err => {
console.log('catch');
return Observable.of(err);
})
.subscribe(data => {
console.log('subscribe');
console.log(data);
});
You can count the number of retries in the retries
variable yourself and if it reaches some limit just rethrow the error. The map()
operator wraps all callbacks with try-catch blocks so any error thrown in its callable is going to be sent as error
signal.
return err.delay(1000).take(5)
There should be a error after retries, for example:
return err.delay(1000).take(5).concat(Observable.throw(err))
Thanks @martin for pointing out that this code actually throws Observable instead of error.