Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
How to set timeout for a fetch in promise.all?

I use Promise.all to fetch 2 request. But My API have rate limit one request per second. So there was an error: the server responded with a status of 429 (Too Many Requests) Is there any way to solve this problem?

const getKeywords = fetch('https://urlmyapi.com').then((res) =>
  res.json().then((json) => {
    if (res.ok) {
      return json
    }
    throw json.message
  })
)

const getProducts = fetch('https://urlmyapi.com').then((res) =>
  res.json().then((json) => {
    if (res.ok) {
      return json
    }
    throw json.message
  })
)

const [keywords, products] = await Promise.all([getKeywords, getProducts])
return {
  keywords,
  products,
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As mentioned in the comments, it sounds like you want to use setTimeout to wait for one second before executing the second request. I think this will work.

async function getKeywords() {
  const res = await fetch("https://urlmyapi.com/keywords");
  const json = await res.json();
  if (res.ok) {
    return json;
  }
  throw new Error(json.message);
}

async function getProducts() {
  const res = await fetch("https://urlmyapi.com/products");
  const json = await res.json();
  if (res.ok) {
    return json;
  }
  throw new Error(json.message);
}

async function getKeywordsAndProducts() {
  // make first request
  const keywords = await getKeywords();

  // pause for one second
  await new Promise((resolve) => setTimeout(resolve, 1000));

  // make second request
  const products = await getProducts();

  return { keywords, products };
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Hey you should use denounce technique in this case by delaying your api calls.

const getKeywords = fetch('https://urlmyapi.com').then((res) =>
  res.json().then((json) => {
    if (res.ok) {
      return json
    }
    throw json.message
  })
)

const getProducts = fetch('https://urlmyapi.com').then((res) =>
  res.json().then((json) => {
    if (res.ok) {
      return json
    }
    throw json.message
  })
)

const reolvePromisesWithDelay=async(promises=[],delay=3000)=>{
  return Promise.all(promises.map((prom, index) => {
    if(index % 2){
      return new Promise(resolve => setTimeout(resolve, delay));
    }else{
      return prom;
    }
  });
}

const [keywords, products] = await reolvePromisesWithDelay([getKeywords, getProducts],4000)
return {
  keywords,
  products,
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

So you don't want to call Promise.all then, because it will fire all function simultaneously. My suggestion is to simply fire one api call then, pause for one second and fire second api call.

Something like this:

const keywords = await getKeywords();
await new Promise(resolve => setTimeout(resolve, 1000));
const products = await getProducts();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda