【发布时间】:2018-02-28 18:17:29
【问题描述】:
每当触发新请求时,我都有一个用例,任何已经在进行中的 http 请求都应该被取消/忽略。
例如:
- 一个请求(比如 #2)进来了,而请求 #1 响应时间过长/网络连接速度慢。
- 在这种情况下,#2 会在任何时候从服务器获得非常快速的响应,即使 #1 返回 HTTP 响应的响应也是如此 observable 应该被忽略。
- 我面临的问题是,首先组件显示来自请求 #2 的响应值,并在请求 #1 完成时再次更新(这不应该发生)。
我认为 switchMap 取消了 obervables / 维护了 observable 值的发出顺序。
摘自我的 service.ts
Obervable.of('myServiceUrl')
.switchMap(url => this.httpRequest(url) )
.subscribe( response => {
// implementation
/** Update an observable with the
with latest changes from response. this will be
displayed in a component as async */
});
private httpRequest(url) {
return this.httpClient.get('myUrl', { observe: 'response' });
}
上述实现不起作用。有人能找出这个用例的正确实现吗?
【问题讨论】:
-
你是如何使用
httpResponse$的? -
我不需要 httpResponse$,因为所有的实现都在 subscribe() 中进行了处理。更新了代码 sn-p。
标签: angular rxjs observable angular-httpclient switchmap