Angular2 / RxJS - how to repeat from within 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() } } ) 

if an exception occurs, it will be sent to the function in the API, and then true will be returned if the problem is fixed (for example, the token has been updated), and it just needs to try again after fixing

I could not figure out how to do this.

+5
source share
2 answers

In your .getCompanies() request, immediately after .map add .retryWhen :

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

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

+6
source

Do you mean something like this?

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

Source: https://habr.com/ru/post/1258602/


All Articles