forkJoin不等待多个Http请求完成

时间:2018-12-18 22:38:54

标签: angular rxjs fork-join

所以我有三个要传递给forkJoin的http请求:

apiRequest1 = this.http.getApi1('...');
// The same format is for the remaining api requests.

forkJoin(apiRequest1, apiRequest2, apiRequest3)
    .subscribe(([results1, results2, results3]) => { rest of code }

results3中的数据一直以空数组的形式返回。如果我自己运行HttpRequest并订阅它,那么数据返回就很好。有什么办法可以解决这个问题?

1 个答案:

答案 0 :(得分:3)

您可以尝试以下方法吗?

forkJoin(
  apiRequest1, apiRequest2, apiRequest3
).subscribe(
    response =>{
      //response[0] is data returned by API apiRequest1
      //response[1] is data returned by API apiRequest2
      //response[2] is data returned by API apiRequest3
    }
    error => console.log("Error: ", error),
    () =>{
      //All the API calls are completed here. Put your code here
      //codes should be executed after the completion of all API calls
    }
)