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

Angular RxJS:在状态码202上重复请求

发布时间:2020-12-17 07:16:11 所属栏目:安全 来源:网络整理
导读:我有一个简单的请求,如果响应状态为202,我想重复一遍.目前,它看起来像这样: this.http.get(endPoint,{ withCredentials: true,headers: this.headers}).map((response: Response) = { return response.text() ? response.json() : '';}).catch((error) = th
我有一个简单的请求,如果响应状态为202,我想重复一遍.目前,它看起来像这样:

this.http.get(endPoint,{
    withCredentials: true,headers: this.headers
})
.map((response: Response) => {
    return response.text() ? response.json() : '';
})
.catch((error) => this.handleError(error));

我尝试过.repeatWhen()但不幸的是,我没有收到Response对象,我无法通过状态代码设置验证.

任何想法我该怎么做?

解决方法

这是对repeatWhen()工作方式的误解.对此运算符的回调接收单个参数,该参数是一个Observable,它在源完成时推送空项.所以它对你来说并不理想(因为你无法访问所需的Response对象).

您可以使用retryWhen()代替:

this.http.get(endPoint,{ withCredentials: true,headers: this.headers })
    .map((response: Response) => {
        if (response.code === 202) {
            throw response;
        }
        return response;
    })
    .map((response: Response) => {
        return response.text() ? response.json() : '';
    })
    .retryWhen(obs => {
        return obs; // always just resubscribe without any further logic
    });

这会将响应作为错误抛出,然后通过重试捕获,只需重新订阅.如果您重新订阅,您当然可以使用更多逻辑来控制,例如:

.retryWhen(obs => {
   return obs.filter(...);
});

(编辑:李大同)

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

    推荐文章
      热点阅读