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

143
Views
Call backup endpoint in case get promise fails

I have a method returning a promise that either resolves or rejects.

myMethod() {
    return new Promise((resolve, reject) => {
      axiosClient
        .get(endpoint)
        .then((response) => {
          resolve(response.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

Is there a way to return another promise in case this produces an error, calling a backup endpoint?? Something along the lines of:

myMethod() {
    return new Promise((resolve, reject) => {
  axiosClient
    .get(endpoint)
    .then((response) => {
      resolve(response.data);
    })
    .catch(() => {
      axiosClient
        .get(backupEndpoint)
        .then((response) => {
          resolve(response.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
});

Edit: if this is a duplicate I have not been able to find something about it that is specific to this point. Could it be because it's a bad practice?

Edit2: thanks for the answers, maybe I should clarify better the flow I want to achieve:

get endpoint -> (if this fails) get fallback endpoint -> (if this fails aswell) reject the entire promise

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From the above, one could simplify it doing with re-usability best practices, as follows:

myMethod() {
  const PromiseHandler = url => {
    /**
    * this function recevies url
    * makes the request and return
    * a reposne form the end point
    */
    return axiosclient.get(url).then(res => res.data)
  }
  
  return PromiseHandler(endpoint)
    .catch(() => {
      return PromiseHandler(backupEndPoint)
}

about 4 years ago · Juan Pablo Isaza Report
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!