angular2/RxJS - 如何从 subscribe() 内部重试

标签 angular rxjs observable angular2-observables

这是我的代码:

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/

相关文章:

javascript - 单击按钮时我没有得到输出

firebase - 将 rxjs Observable 与 firestore 一起使用

Android 将应用程序注册为 LiveData 中的上下文

javascript - RxJS 监听事件但稍后附加事件监听器

angular - 如何从 Observable.from 收集发射值数组?

java - 使用Observable.interval等待远程数据或超时

Angular2 路由器 - 无法使 resetConfig 工作

javascript - 错误 TS2315 : Type 'ElementRef' is not generic material angular

angular - 如何将 Angular cli 运行到 https ://localhost:4200 SSL mode

angular - 根据响应递归组合 HTTP 结果