Home > Mobile >  Call backup endpoint in case get promise fails
Call backup endpoint in case get promise fails

Time:02-05

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

CodePudding user response:

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)
}

CodePudding user response:

First of all, axios.get already returns a promise, so no need to explicitly creating a promise using the promise constructor.

As far as your question is concerned, you could return the result of calling axios.get(...) from the catch block as shown below.

myMethod() {
  return axiosClient
    .get(endpoint)
    .catch(() => {
      return axiosClient.get(backupEndpoint);
    })
    .then((response) => {
       return response.data;
    });
}

Returning a value from the callback function of the catch method resolves the promise returned by the catch method with the value returned by the callback function.

In your case, you will return a promise, i.e. axios.get(...).then(...) from the callback function of the catch method, so the promise returned by the catch method will be resolved to the promise returned by the axios.get(...).then(...).

If axios.get(...).then(...) is resolved, promise returned by the catch method will also get resolved. Similarly, if axios.get(...).then(...) gets rejected, promise returned by the catch method will also get rejected.

In other words, fate of the promise returned by catch will now depend on the fate of the promise returned by axios.get(...).then(...).

To avoid repetition, above method could be written as shown below:

myMethod() {
  return axiosClient
    .get(endpoint)
    .catch(() => {
      return axiosClient.get(backupEndpoint);
    });
}
  •  Tags:  
  • Related