这是我的代码:
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() } }
)
万一发生异常,它将被发送到 API 中的一个函数,如果问题得到解决(例如 token 刷新),它将返回 true
并且它只需要在它之后重试固定
我不知道如何让它重试。
最佳答案
在你的.getCompanies()
在 .map
之后立即调用添加 .retryWhen
:
.retryWhen((errors) => {
return errors.scan((errorCount, err) => errorCount + 1, 0)
.takeWhile((errorCount) => errorCount < 2);
});
在此示例中,可观察对象在 2 次失败后完成 (errorCount < 2
)。
关于angular2/RxJS - 如何从 subscribe() 内部重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175255/