gpt4 book ai didi

Angular 7 在触发下一个之前等待 http 网络调用完成

转载 作者:搜寻专家 更新时间:2023-10-30 21:44:33 25 4
gpt4 key购买 nike

我有一个数据服务,可以在其他服务需要时定期触发 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 已触发请求但尚未完成。

最佳答案

执行此操作的 rxjs 方法是使用 concatMapTo

作为stackblitz example显示

const fakeRequest = of('Network request complete').pipe(delay(3000));
//wait for first to complete before next is subscribed
const example = sampleInterval.pipe(concatMapTo(fakeRequest));
//result
//output: Network request complete...3s...Network request complete'
const subscribe = example.subscribe(val => console.log(val));

关于Angular 7 在触发下一个之前等待 http 网络调用完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57810500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com