How to slowdown making requests?

I have problem with too fast requesting. When my script makes too many requests per second Google throws an error net::ERR_INSUFFICIENT_RESOURCES

I want to make one request per 20ms.
How I can achieve that?


This is how my main function look at this moment…

const symbolsArr = reader.result.split("\n").map((str) => str.trim()); //symbolsArr is just rergular array, except it's source is from .txt file
    function loopFunction(error_symbol) {
      for (const symbol of symbolsArr) {
        setTimeout(getData(symbol), 5000); //I tried to use setTimeout but it not helps
      }
      return console.log(error_symbol);
    }
    loopFunction(error_symbols);

And my fetcher…

error_symbols = [];

function getData(symbol) {
  fetch(
    `https://cors-anywhere.herokuapp.com/` +
      `https://eodhistoricaldata.com/api/fundamentals/${symbol}?api_token= (don't look at my secret token :))`,
    {
      method: "get",
    }
  )
    .then((res) => {
      if (res.ok) {
        return res.json();
      } else {
        throw new Error(`Symbol ${symbol} is 'empty'`);
      }
    })
    .then((data) => {
      console.log(data);
      var myJSON = JSON.stringify(data);
      saveFile(myJSON, `${symbol}-json.txt`, "text/plain");
    })
    .catch((error) => {
      error_symbols.push(symbol);
      throw error + symbol;
    });
}

Pretty simple, I have to somehow put cooldown on fetcher

94 thoughts on “How to slowdown making requests?”

  1. Excellent blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option? There are
    so many options out there that I’m totally confused ..
    Any suggestions? Kudos! http://cleckleyfloors.com/

    Reply
  2. Pingback: ivermectil use
  3. Pingback: dapoxetine 60mg uk

Leave a Comment