简体   繁体   English

我需要限制这个 for 循环发送的请求数量

[英]I need to throttle the amount of requests this for loop sends

I'm fetching requests but I am getting the 'sent to many requests error', how can I throttle the amount of requests this loop is sending to 10 requests a second?我正在获取请求,但出现“发送到许多请求错误”,如何将这个循环发送的请求数量限制为每秒 10 个请求?

let accesstoken = token2.access_token; 
const promises = [];
for(var i = 0; i < 5;i++){
promises.push(fetch('https://us.api.blizzard.com/profile/wow/character/'+ slug[i]+'/'+ lowercasenames[i] +'?namespace=profile-us&locale=en_US', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${accesstoken}`
    }
  })
  .then(logresponse => { console.log(logresponse.status); return logresponse})
  .then(response => response.json())
  .catch(error => console.log(error)))
  
}
  Promise.all(promises)
  .then(data => { profiledata(data)})

};```

Make sure you call throttledRequest in the scope that is invoking all of the request and not for each character/accessToken/slug so it will take all the request into count确保在调用所有请求的范围内调用 throttledRequest,而不是为每个字符/accessToken/slug 调用,这样它将计算所有请求

// a function that throttled the request up to 10 requests a second
const throttledRequest = () => {
  const requestCount = 0;

  const fetchRequest = (accessToken, slug, name) =>
    fetch(
      `https://us.api.blizzard.com/profile/wow/character/${slug}/${name}?namespace=profile-us&locale=en_US`,
      {
        method: "GET",
        headers: {
          Authorization: `Bearer ${accessToken}`,
          "Content-Type": "application/json",
        },
      }
    ).then((response) => {
      console.log(response.status);
      return response.json();
    }).catch(err=>console.error(err));

  return (accessToken, slug, name) => {
    if (requestCount < 10) {
      requestCount++;
      return fetchRequest(accessToken, slug, name);
    } else {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          requestCount=0;
          resolve(fetchRequest(accessToken, slug, name));
        }, 1000);
      });
    }
  };
};

const runWithAsyncAwait = async () => {
  const request = throttledRequest();
  for (let i = 0; i < 5; i++) {
    const data = await request("accessToken", slug[i], "name");
    profiledata(data);
  }
};
const runWithPromises = async () => {
  const request = throttledRequest();
  for (let i = 0; i < 5; i++) {
    request("accessToken", slug[i], "name").then((data) => profiledata(data));
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM