加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:42:12 所属栏目:安全 来源:网络整理
导读:这是我的代码: 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中
这是我的代码:

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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读