【问题标题】:How to fetch multiple multiple data from multiple endpoints inside Promise.all?如何从 Promise.all 中的多个端点获取多个数据?
【发布时间】:2021-11-13 13:54:44
【问题描述】:

我试图从 Promise.all 中的多个 fetch GET 调用中获取多个数据,而不必为每个 Fetch 编写 .then 和 .catch。 这是我编写的代码,它正在记录数据,但仍处于承诺状态。 .json() 好像不能这样工作。

Promise.all([
  fetch(allAirportsURL, bearer), 
  fetch(allFlightsURL, bearer)
  ])
  .then(([responseOne, responseTwo]) => {
    return [responseOne.json(), responseTwo.json()];
  })
  .then(([dataOne, dataTwo]) => {
    console.log('data', dataOne);
  })
  .catch((err) => {
    console.log(err);
  });

有什么建议吗?

【问题讨论】:

标签: reactjs promise get fetch


【解决方案1】:

尝试先返回Promise.all([responseOne.json(), responseTwo.json()]),然后再返回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2021-08-06
    相关资源
    最近更新 更多