I have app where i have function get()
which returning me Observable.interval(1000)
with http GET.
我有应用程序,我有函数get(),它返回我与http GET Observable.interval(1000)。
In app.component I subscribe this Observable and printing on screen some values from GET.
在app.component中,我订阅了这个Observable并在屏幕上打印了一些来自GET的值。
Everythings works fine until the get response returns 400 - the server send 404 from time to time- its normally. But when i recive 400, my subscribe stops working. Screen show only last valuse and stop "refreshing"
Everythings工作正常,直到get响应返回400 - 服务器不时地发送404-正常。但是当我收到400时,我的订阅停止了工作。屏幕显示最后一个valuse并停止“刷新”
Here is some code:
这是一些代码:
getAct(symbol:string): Observable<Act> {
return Observable
.interval(1000)
.flatMap(() => {
return this.http.get(url)
.map((res: Response):Act => (res.json()))
.catch(err => {
return Observable.throw(err);
});
});
}
}
And here is code which subscribe:
以下是订阅的代码:
this.actSub = this.actService
.getAct(this.symbol)
.subscribe(
act => {
console.log("ok");
this.act = act;
}
},
err => {console.warn(err);},
()=>console.log("Done")
);
So, if I run my app in console i have this:
所以,如果我在控制台中运行我的应用程序我有这个:
So the program run fine - 29 get succesfull, but when it recive 400 its stops, and there where no more "ok".
所以程序运行良好 - 29成功,但当它恢复400停止,并在那里没有更多“确定”。
Where is the problem? Is my getAct() wrong, or maybe when i subscribe I code something bad?
哪里有问题?我的getAct()是错误的,或者当我订阅我编码的东西不好?
Please help, I tried to repair this, but i couldn't.
请帮助,我试着修复这个,但我不能。
2 个解决方案
#1
1
There are several RxJS operators to handle errors.
有几个RxJS运算符来处理错误。
The easiest would be to use retry():
最简单的方法是使用retry():
return this.http.get(url)
.retry()
.map(res => res.json());
This is an agressive strategy as it will re-subscribe to the original observable (this.http.get(...)
) as soon as a request fails. You could end up hitting the server really hard.
这是一个激进的策略,因为一旦请求失败,它将重新订阅原始的observable(this.http.get(...))。你最终可能会非常努力地击中服务器。
You might wanna use retryWhen() instead, which will let you introduce a slight delay before retrying, e.g.:
您可能想要使用retryWhen()代替,这会让您在重试之前引入一点延迟,例如:
return this.http.get(url)
.retryWhen(errors => errors.delay(2000)) // 2-second delay before retrying
.map(res => res.json());
#2
0
I can make something like this,but i think it's really bad solution.
我可以做这样的事情,但我认为这是非常糟糕的解决方案。
get(){
this.actSub = this.actService
.getAct(this.symbol)
.subscribe(
act => {
console.log("ok");
this.act = act;
}
},
err => {
console.warn(err);
this.get();
},
()=>console.log("Done")
);
}
#1
1
There are several RxJS operators to handle errors.
有几个RxJS运算符来处理错误。
The easiest would be to use retry():
最简单的方法是使用retry():
return this.http.get(url)
.retry()
.map(res => res.json());
This is an agressive strategy as it will re-subscribe to the original observable (this.http.get(...)
) as soon as a request fails. You could end up hitting the server really hard.
这是一个激进的策略,因为一旦请求失败,它将重新订阅原始的observable(this.http.get(...))。你最终可能会非常努力地击中服务器。
You might wanna use retryWhen() instead, which will let you introduce a slight delay before retrying, e.g.:
您可能想要使用retryWhen()代替,这会让您在重试之前引入一点延迟,例如:
return this.http.get(url)
.retryWhen(errors => errors.delay(2000)) // 2-second delay before retrying
.map(res => res.json());
#2
0
I can make something like this,but i think it's really bad solution.
我可以做这样的事情,但我认为这是非常糟糕的解决方案。
get(){
this.actSub = this.actService
.getAct(this.symbol)
.subscribe(
act => {
console.log("ok");
this.act = act;
}
},
err => {
console.warn(err);
this.get();
},
()=>console.log("Done")
);
}