Search code examples
angularrxjsfork-join

forkJoin not waiting for multiple Http requests to finish


So I have three http requests that I am passing to 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 }

the data in results3 keeps coming back as an empty array. If I run the HttpRequest by itself and subscribe to it, the data comes back just fine. Is there any way I can fix this?


Solution

  • Can you try the below:

    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
        }
    )