angular2/RxJS-如何从内部Subscribe()重试 [英] angular2 / RxJS - how to retry from inside subscribe()

查看:143
本文介绍了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,并且只需要在解决该问题后重试

in case an exception happened, it will be sent to a function in the API then return true if the problem is fixed (like token refreshed for example) and it just needs to retry again after its fixed

我不知道如何重试.

推荐答案

.map之后的.getCompanies()调用中,添加

In your .getCompanies() call right after the .map add a .retryWhen:

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

在此示例中,可观察对象在2次失败(errorCount < 2)之后完成.

In this example, the observable completes after 2 failures (errorCount < 2).

这篇关于angular2/RxJS-如何从内部Subscribe()重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆