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

62
Views
best way to handle backup API calls

I want get data from an API. This API has multiple URLs (for backup). Now I want to request URL1 first, if that returns data: Don't try the backup URLs. If URL doesn't return data, keep on trying. Here is what I have (and that seems to work as far as I can tell) but it looks really stupid:

const getData = async () => {
  let returnData = null;

  try {
    returnData = await axios.get(`${URL[0]}/${path}`);
  } catch (error) {
    console.error(`Error fetching data: ${URL[0]}`, error);
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[1]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[1]}`, error);
    }
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[2]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[2]}`, error);
    }
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[3]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[3]}`, error);
    }
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[4]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[4]}`, error);
    }
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[5]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[5]}`, error);
    }
  }

  if (!returnData) {
    try {
      returnData = await axios.get(`${URL[6]}/${path}`);
    } catch (error) {
      console.error(`Error fetching data: ${URL[6]}`, error);
      throw new Error('last try fetching data:', error);
    }
  }

  return returnData;
}

What is the way to go here?

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!