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

前端之家收集整理的这篇文章主要介绍了angular2 / RxJS – 如何从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中的函数,然后如果问题得到修复则返回true(例如,令牌刷新),并且只需要在修复后再次重试

我无法弄清楚如何让它重试.

解决方法

在你的.getCompanies()调用之后.map添加一个 .retryWhen

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

在此示例中,observable在2次失败后完成(errorCount< 2).

猜你在找的Angularjs相关文章