Angular 角度5+;可观测的forkJoin和可观测的链式并行操作?

Angular 角度5+;可观测的forkJoin和可观测的链式并行操作?,angular,asynchronous,parallel-processing,observable,Angular,Asynchronous,Parallel Processing,Observable,我在angular 5中有两个不同的httpClient请求实现 在一种方法中,我在一个方法中有一个http请求,在该方法中,我在.subscribe()方法中对另一个表再次执行第二个(嵌套)http请求,以从两个不同的表检索数据: this.http.get<any>(someUrl, {observe: 'response'} ).subscribe(fp_resp => { if (fp_resp.status === 200) { this.predic

我在angular 5中有两个不同的httpClient请求实现

在一种方法中,我在一个方法中有一个http请求,在该方法中,我在.subscribe()方法中对另一个表再次执行第二个(嵌套)http请求,以从两个不同的表检索数据:

this.http.get<any>(someUrl, {observe: 'response'}
  ).subscribe(fp_resp => {
  if (fp_resp.status === 200) {
    this.predictionData = this.chart_PrognoseService.formatData(fp_resp.body);
}

    this.http.get<any>(anotherUrl, {observe: 'response'}
    ).subscribe(resp => {
      if (resp.status === 200) {
          this.chart_LoadFactorService.computeLoadFactors(fp_resp.body, resp.body);
      } else {
        console.log('error bei verbindung');
      }
    });
我现在的问题是:在我的第一种方法中,http请求是否仍然异步执行,这意味着某种“并行”?或者不是因为我在另一个请求中执行了一个请求?当我使用forkJoin时,这两个请求是并行执行的吗? 哪种方法更好

在我的第一种方法中,http请求是否仍然异步执行,这意味着某种“并行”?或者不是因为我在另一个请求中执行了一个请求

它们是串行执行的,从逻辑上和语言的工作方式来看,其他任何事情都是不可能的:函数只在第一个请求执行后才被调用

当我使用forkJoin时,这两个请求是并行执行的吗

是的,forkJoin将并行订阅可观察对象,这意味着请求将并行执行

哪种方法更好

您是需要以串联方式做事情,还是可以以并行方式做事情,取决于第二个请求是否依赖于第一个请求的结果,并且通常取决于用例

也就是说,嵌套订阅在rxjs中被认为是一种反模式。如果你想做一系列的事情,你应该使用其他操作符,比如switchMap、mergeMap、concatMap等等,这取决于你的用例。下面是一个简单的原型示例,它以串联方式执行两件事情:

this.http.get(/* first request */).pipe(
  switchMap(result1 => this.http.get(/* second request */))
).subscribe(result2 => {
  // …
});
this.http.get(/* first request */).pipe(
  switchMap(result1 => this.http.get(/* second request */))
).subscribe(result2 => {
  // …
});