angular2 / RxJS - 如何从 subscribe() 内部重试
angular2 / RxJS - how to retry from inside subscribe()
这是我的代码:
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 中的函数,然后 return true
如果问题已解决(例如令牌刷新)并且它只需要在修复后重试
我不知道如何让它重试。
你的意思是这样的?
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)
}
}
}
在您的 .getCompanies()
调用中 .map
之后添加一个 .retryWhen
:
.retryWhen((errors) => {
return errors.scan((errorCount, err) => errorCount + 1, 0)
.takeWhile((errorCount) => errorCount < 2);
});
在此示例中,可观察对象在 2 次失败后完成 (errorCount < 2
)。
这是我的代码:
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 中的函数,然后 return true
如果问题已解决(例如令牌刷新)并且它只需要在修复后重试
我不知道如何让它重试。
你的意思是这样的?
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)
}
}
}
在您的 .getCompanies()
调用中 .map
之后添加一个 .retryWhen
:
.retryWhen((errors) => {
return errors.scan((errorCount, err) => errorCount + 1, 0)
.takeWhile((errorCount) => errorCount < 2);
});
在此示例中,可观察对象在 2 次失败后完成 (errorCount < 2
)。