Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
How do I limit fetch calls made to an endpoint service so that no more than 10 calls per second are made?

In order to avoid the "Error 429 Too Many Requests" error in browser, how can I modify the below code so that no more than 10 calls per second are made?

This 10 calls per second rate limit is imposed by a 3rd-party API, over which we have no control.

Thanks in advance!

// Array of hundreds of URLS
const urls = allNames.map(
        (name) =>
`https://example.com/api/v3/results/${name}?limit=120&apikey=123`
    );

// TODO add throttle so as to not exceed 10 calls per second
const getStaticProps = async () => {
            Promise.allSettled(
            urls.map((url) =>
                fetch(url).then((resp) => resp.json())
            )
        ).then(console.log);
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can fetch ten and wait for one second.

// Array of hundreds of URLS
const urlsList = allNames.map(
        (name) =>
`https://example.com/api/v3/results/${name}?limit=120&apikey=123`
    );

// TODO add throttle so as to not exceed 10 calls per second
// Fuction that returns a Promise that resolves after given Milliseconds
const timer = ms => new Promise(res => setTimeout(res, ms))

for (let i = 0; i < urlsList.length; i+=10) {
  getStaticProps(urlsList.slice(i-10, i));
  await timer(1000); //It will make 10 calls and wait 1 seconds
} 

const getStaticProps = async (urls) => {
            Promise.allSettled(
            urls.map((url) =>
                fetch(url).then((resp) => resp.json())
            )
        ).then(console.log);
};

about 4 years ago · Juan Pablo Isaza Relatório

0

Hopefully this helps :)

const urls = allNames.map(
  (name) => `https://example.com/api/v3/results/${name}?limit=120&apikey=123`
);

// 1s === 1000ms
const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const isEven = (num) => num % 2 === 0;

const getStaticProps = async () => {
  const response = await Promise.allSettled(
    urls.map(async (url, index) => {
      if (index && isEven(index / 5)) await delay(1000);
      return await fetch(url).then((res) => res.json());
    })
  );
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda