forkJoin 不等待多个 Http 请求完成

Jas*_*ca1 0 fork-join rxjs angular

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

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

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

结果 3 中的数据不断作为空数组返回。如果我自己运行 HttpRequest 并订阅它,数据就会恢复正常。有什么办法可以解决这个问题吗?

Kev*_*vin 5

你可以试试下面的:

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
    }
)
Run Code Online (Sandbox Code Playgroud)