Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在循环JS中获取Fetch_Javascript_For Loop_Fetch Api - Fatal编程技术网

Javascript 在循环JS中获取Fetch

Javascript 在循环JS中获取Fetch,javascript,for-loop,fetch-api,Javascript,For Loop,Fetch Api,问题是,我怎样才能避免打300次电话给second fetch?还是有别的办法,我在做什么? 另外,如何对第一个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 re

问题是,我怎样才能避免打300次电话给second fetch?还是有别的办法,我在做什么? 另外,如何对第一个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);            
    }); 
};
for(设i=1;i{
if(response.ok)返回response.json();
抛出新错误(response.statusText);
})
.then(函数句柄数据){
return fetch('example.api')//应返回1次
。然后(响应=>{
if(response.ok)返回response.json();
抛出新错误(response.statusText);
})
})
.catch(函数句柄错误){
console.log(“错误”+错误);
}); 
};

您可以使用Promise all解决此问题

let promises = [];
for (let i = 1; i <= 300; i++) {
  promises.push(fetch(`example.api/incomes/${i}`));
}
Promise.all(promises)
  .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);
  });
let promissions=[];
for(设i=1;i{
if(response.ok)返回response.json();
抛出新错误(response.statusText);
});
})
.catch(函数句柄错误){
console.log(“错误”+错误);
});

您可以使用Promise all解决此问题

let promises = [];
for (let i = 1; i <= 300; i++) {
  promises.push(fetch(`example.api/incomes/${i}`));
}
Promise.all(promises)
  .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);
  });
let promissions=[];
for(设i=1;i{
if(response.ok)返回response.json();
抛出新错误(response.statusText);
});
})
.catch(函数句柄错误){
console.log(“错误”+错误);
});

将所有请求存储在一个数组中。然后使用
Promise.all()
等待所有这些请求完成。然后,当所有请求完成后,使用另一个
Promise.all()
,其中包含
map()
,返回每个请求的JSON,并等待所有请求完成

现在,您的
数据
参数将在下一次
然后
回调中提供一个对象数组

function fetch300Times() {
  let responses = [];
  for(let i = 1; i <= 300; i++) {.
    let response = fetch(`example.api/incomes/${i}`);
    responses.push(response);
  } 
  return Promise.all(responses);
}

const awaitJson = (response) => Promise.all(responses.map(response => {
  if(response.ok) return response.json();
  throw new Error(response.statusText);
}));

fetch300Times()
  .then(awaitJson)
  .then(data => {
    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次(){ 让响应=[]; for(让i=1;我保证.all(response.map)(response=>{ if(response.ok)返回response.json(); 抛出新错误(response.statusText); })); 获取300次() .然后(等待JSON) 。然后(数据=>{ fetch('example.api')//应返回1次 。然后(响应=>{ if(response.ok)返回response.json(); 抛出新错误(response.statusText); }); }).catch(函数句柄错误){ console.log(“错误”+错误); });
将所有请求存储在一个数组中。然后使用
Promise.all()
等待所有这些请求完成。然后在所有请求完成后,使用另一个
Promise.all()
,其中包含
map()
,返回每个请求的JSON并等待所有请求完成

现在,您的
数据
参数将在下一次
然后
回调中提供一个对象数组

function fetch300Times() {
  let responses = [];
  for(let i = 1; i <= 300; i++) {.
    let response = fetch(`example.api/incomes/${i}`);
    responses.push(response);
  } 
  return Promise.all(responses);
}

const awaitJson = (response) => Promise.all(responses.map(response => {
  if(response.ok) return response.json();
  throw new Error(response.statusText);
}));

fetch300Times()
  .then(awaitJson)
  .then(data => {
    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次(){ 让响应=[]; for(让i=1;我保证.all(response.map)(response=>{ if(response.ok)返回response.json(); 抛出新错误(response.statusText); })); 获取300次() .然后(等待JSON) 。然后(数据=>{ fetch('example.api')//应返回1次 。然后(响应=>{ if(response.ok)返回response.json(); 抛出新错误(response.statusText); }); }).catch(函数句柄错误){ console.log(“错误”+错误); });
所以你想调用第一个
获取
300次,然后在所有获取完成后,调用第二个?如果你能控制API,我建议你实现某种批处理机制。300次API调用只会减慢你所做的一切,并使你的API服务器过载。是的,他们应该可以访问retr查看彼此的数据。第一次获取是一个300个对象,应该与第二次获取数组中项目的ID对应。@MaazsYedEdeeb它
s类似于相同的API,但每个项目位于不同的API地址下。因此,我
m使用{i}参数来访问它。因此,您希望调用第一个
fetch
300次,然后在所有获取完成后,调用第二个?如果您对API有控制权,我建议您实现某种批处理机制。300次API调用只会减慢您所做的一切,并使您的API服务器过载。是的,它们是应该有权检索彼此的数据。第一个fetch是一个300个对象,应该与第二个fetch数组中的一个项的ID对应。@MaazsYedEdeeb it
s类似于相同的API,但每个项位于不同的API地址下。因此我使用{i}参数来访问它。