【发布时间】:2020-06-27 19:52:45
【问题描述】:
问题是,我怎样才能摆脱调用 second fetch 300 次?还是有其他方法可以做到这一点,我在做什么? 此外,如何对第一个 api 进行有序(不想排序)调用,因为它们以混乱的异步方式来自 api?
for(let i=1;i<=300; i++) {
fetch(`example.api/incomes/${i}`) // should be returned 300 times
.then(response => {
if(response.ok) return response.json();
throw new Error(response.statusText);
})
.then(function handleData(data) {
return fetch('example.api') // should be returned 1 time
.then(response => {
if(response.ok) return response.json();
throw new Error(response.statusText);
})
})
.catch(function handleError(error) {
console.log("Error" +error);
});
};
【问题讨论】:
-
所以你想调用第一个
fetch300 次,然后在所有提取完成后调用第二个? -
如果您可以控制 API,我建议您实现某种批处理机制。 300 次 API 调用只会减慢您正在执行的操作,并使您的 API 服务器过载。
-
是的,他们应该有权检索彼此的数据。第一次提取是 300 个对象,应该绑定对应于第二次提取数组中项目的 ID。
-
@MaazSyedAdeeb it
s like same API, but each item lays down under different api adress. So im 使用 {i} 参数来访问它。
标签: javascript for-loop fetch-api