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

502
Vistas
How can I make Axios try with multiple urls and if one of them working then return data

Like the title, I'm working on an app that calls the YouTube APIs but sometimes my app makes too many requests that trigger the API quotas limit and thus making the app stop working due to error on the API server. The solution for this which I found on the Internet is to create multiple API keys and looping through them. But I'm having a headache figuring out how to make my Axios trying multiple API URLs before returning the data. Any tips or tricks on this?

Here is the sample code I tried:

async function getYoutubeVideoSnippet(id) {
  const response = await axios.all([
    axios.get(
      "https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=" +
        id +
        "&key=API_KEY"
    ),
    axios.get(
      "https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=" +
        id +
        "&key=API_KEY"
    ),
  ]);
  return response.data;
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I don't understand exactly what you want to do, but If you want to have many requests and only need one response, You can use Promise.any. For example

async function fetchData () {
   const requests = [
     axios.get('...'),
     axios.get('...'),
   ]
   const response = await Promise.any(requests)
   return response.data
}

With this example, You can receive the first response, but you should be aware that all requests are eventually sent

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