【发布时间】: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);
});
有什么建议吗?
【问题讨论】:
-
是的,不要在每次提取时写
.then()- 写 one helper function to do that and also handle HTTP errors,然后调用两次。