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

126
Vistas
Promises making API calls in JavaScript with async/await syntax

Trying to get a better grasp on Promises with async/await syntax. .then().catch() feels more straightforward with the added visual cues and structure. Do these two async/await functions make any logical sense for making multiple API calls and acting on the data once they're resolved? The goal is to have a function that takes the url, number of additional pages and returns data from all calls in one output. It works but is slow enough to lead me to believe its pretty inefficient. (I'm sure the nested loops dont help)

async function getData(url, numOfPages) {
  try {
    const response = await axios.get(url)
    const { results, next: nextUrl } = response.data

    const nextPages = await getNextPages(nextUrl, numOfPages)

    const finalData = results.concat(nextPages)

    finalData.forEach(data => console.log(data.name))
  }
  catch (err) {
    console.log(err)
  }
}
getData('https://swapi.dev/api/planets/', 3)


async function getNextPages(nextUrl, numOfPages) {
  try {
    const response = await axios.get(nextUrl)
    const data = response.data.results.map(res => res)

    let nextPage = response.data.next

    for (let i = 1; i < numOfPages; i++) {
      let response = await axios.get(nextUrl)
      let results = response.data.results
      nextPage = response.data.next

      for (let i = 0; i < results.length; i++) {
        data.push(results[i])
      }
    }
    return data
  }
  catch (err) {
    console.log(err)
  }
}
about 4 years ago · Juan Pablo Isaza
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