代码之家  ›  专栏  ›  技术社区  ›  Jasonca1

forkJoin未等待多个Http请求完成

  •  0
  • Jasonca1  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  5
  •   Kevin    7 年前

    你能试试下面的方法吗

    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
        }
    )
    
    推荐文章