【发布时间】:2020-01-08 15:39:18
【问题描述】:
我有一个数据服务,它会在其他服务需要时定期触发 http 调用。现在,此服务异步工作,因此可能会在前一个尚未完成时请求数据服务触发 http 调用。
我想知道当我需要进行 http 呼叫时,如何使用 rxjs 检查是否有正在进行的呼叫
数据服务:
constructor(private http: HttpClient){}
// if this gets called while another hasn't returned, wait for it and then trigger the next http call
public request(method, url, options): Observable<any>{
return this.http.request(method, url, options);
}
服务 A:
public syncA(){
setTimeout(() => {
this.dataService.request('GET', 'someUrl', someOptions).subscribe((response) => {
console.log('periodic call returns ', response});
}, 45000);
}
服务 B:
public doB(): Observable<any>{
return this.dataService.request('GET', 'someUrl', someOptions)
}
这种情况是服务 B 调用 doB 而 syncA 已触发请求但尚未完成。
【问题讨论】:
-
显而易见的答案是从第一个请求的
subscribe回调中使用第二个请求。这不适用于您的用例吗? -
@TheHeadRush 你能提供一个样本来说明这将如何?示例被简化,在实际应用中它们可能是多个服务,在不同的时刻触发 https 调用,有的每隔一定时间触发一次,有的由用户明确决定
-
确实,A 和 B 只是对他们拨打的电话感兴趣,他们不需要知道其他正在处理数据服务的事情,这个应该处理他们等待的请求在触发下一个之前完成...如果他们听取所有响应,他们将收到他们不感兴趣的信息,当其他服务从其他服务完成时
标签: angular typescript rxjs rxjs-observables