Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!