当HTTP请求失败时,我想重试两次,间隔1秒。如果它第三次失败,我想把这个错误转发给观察者。最后那部分我有麻烦了。
DataService.get()的HTTP请求
return this.http.get(url,options)
.retryWhen(errors => errors.delay(1000).take(2))
.catch((res)=>this.handleError(res));
订阅
this.dataSvc.get('/path').subscribe(
res => console.log(res),
err => console.error(err),
() => console.log('Complete')
);
我的服务器设置为总是返回一个错误(状态400 Bad request
)。
this.handleError()
捕获的错误Angular 2 rc.6
,RxJS 5 beta 11
,Typescript 2.0.2
发布于 2016-09-13 09:10:50
我用了 operator
return this.http.get(url,options)
.retryWhen(errors => errors.delay(1000).scan((acc,source,index)=>{
if(index) throw source;
}))
.catch((res)=>this.handleError(res));
scan()
参数
acc
:累加器(想想Array.reduce()
)。如果修改并返回它,新值将在下一次执行中作为acc
参数提供。source
:由上一次操作(delay()
本身从errors
转发它)发出的值(或异常)index
:当前发出的值的索引(基于零)这就产生了3个HTTP请求(不知道为什么;我本来以为会有2个)。在第三次失败时,它抛出source
--发出的错误--将被handleError()
捕获。
https://stackoverflow.com/questions/39465733
复制相似问题