【问题标题】:forkJoin not waiting for multiple Http requests to finishforkJoin 不等待多个 Http 请求完成
【发布时间】:2019-05-19 09:50:09
【问题描述】:

所以我有三个 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 }

results3 中的数据不断返回为空数组。如果我自己运行 HttpRequest 并订阅它,数据就会很好地返回。有什么办法可以解决这个问题吗?

【问题讨论】:

  • forkJoin 期望传递的 observables 完成——如果它们没有完成——不行。
  • 你能从 API 请求中发布更多内容吗?这可能与方法中的this 范围有关。 forkJoin 本身没有问题。
  • @Rafael 我知道。这就是我想在实例中使用 forkJoin 的原因。我希望在处理订阅括号下方的任何进一步代码之前完成所有三个 http 调用。奇怪的是,当我自己而不是在 forkJoin 中运行第三个 api 调用时,它完成得很好。需要注意的是,forkJoin 中的所有三个 api 调用都完成了(我在控制台中看到它),这只是出于某种原因在处理订阅方法调用中的代码之前,forkJoin 本身不会等待最后一个完成。
  • 它一定是错误完成的,但是我们不看它的代码怎么知道!?

标签: angular rxjs fork-join


【解决方案1】:

你可以试试下面的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    相关资源
    最近更新 更多