Search code examples
node.jsfetchthrottling

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?

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)})

};```

Solution

  • 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

    // 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));
      }
    };