Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 / RxJS - how to retry from inside subscribe()

this is my code:

this._api.getCompanies().subscribe(
    res => this.companies = JSON.parse(res),
    exception => {if(this._api.responseErrorProcess(exception)) { // in case this retured TRUE then I need to retry() } }
)

in case an exception happened, it will be sent to a function in the API then return true if the problem is fixed (like token refreshed for example) and it just needs to retry again after its fixed

I could not figure out how to make it retry.

like image 250
Motassem MK Avatar asked Oct 21 '16 11:10

Motassem MK


1 Answers

In your .getCompanies() call right after the .map add a .retryWhen:

.retryWhen((errors) => {
    return errors.scan((errorCount, err) => errorCount + 1, 0)
                 .takeWhile((errorCount) => errorCount < 2);
});

In this example, the observable completes after 2 failures (errorCount < 2).

like image 145
rinukkusu Avatar answered Nov 29 '22 06:11

rinukkusu