I am trying to use retryWhenin HTTP calls.
It works great when trying to use like this:
return this.http.get(`${environment.apiUrl}/track/${this.user.instance._id}/${this.currentPlayer.playlist.id}/next?s=${this.playerCounter}`, options)
.timeout(500, new TimeoutError(`Timeout trying to get next track. [instanceId=${this.user.instance._id}]`))
.retryWhen(attempts => {
return Observable.range(1, 3).zip(attempts, i => i).flatMap(i => 3 === i ? Observable.throw(attempts) : Observable.timer(i * 1000));
})
It makes a maximum of 3 attempts if it receives a timeout error.
But I always have buuut, I want to make it more abstract for use in various use cases, and for this I need to check the type of error.
Only TechnicalErros will be re-reviewed.
So, I tried this without success.
.retryWhen(attempts => {
return attempts.flatMap(error => {
if(error instanceof TechnicalError) {
return Observable.range(1, 3).zip(attempts, i => i).flatMap(i => 3 === i ? Observable.throw(attempts) : Observable.timer(i * 1000));
} else {
Observable.throw(error);
}
});
})
He stops on the first try and does not execute Observable.timer(), either Observable.throw().
I am almost sure that the problem is in the first flatMap, I have already tried to use it mergeMapwithout success.
Thanks in advance!