【发布时间】:2018-06-27 10:42:24
【问题描述】:
我有这项服务,最初返回 null(初始化后台作业)[ this.q = true]。但是稍后如果我将其更改为 [ this.q = false] 并且如果作业成功,它将返回值。
所以,首先我需要致电[ this.q = true] 并重试[ this.q = false]。
但是下面的代码不起作用。它总是让https://api.example.com?q=true
return this.http.get('https://api.example.com?q=' + this.q)
.map((res: any) => {
if (res === null) {
throw new Error("not enough tiles !");
}
return res;
}).catch((e) => {
this.q = false;
return error;
})
.retryWhen(e => e.delay(2000))
.retry(3);
【问题讨论】:
-
你在 "catch" 中有 this.q=false,而不是在 if(res===null){this.q=true;...} 中。响应 null 可以是有效的
-
response null 是有效的,这就是为什么我在
res === null将控件发送到 catch 时抛出错误的原因。我放了控制台,它就被执行了。 -
第一个请求
https://api.example.com?q=true它将返回 null。在那之后,我必须用https://api.example.com?q=false重试。 -
我认为 throw new Error("..") 不会让程序去“catch”,但我不确定