What is the proper way to handle errors from nested promises?
Setup
new Promise((resolve, reject) => {
// literally anything
httpCommonServiceAPI
.getDataWithoutParams(url)
.then((response) => resolve(response.data))
.catch((error) => reject(error))
});
httpCommonServiceAPI.getDataWithoutParams = (url) =>
new Promise((resolve, reject) =>
Axios.get(url)
.then((response) => resolve(response.data))
.catch((error) => reject(error));
I know if I replace the second promise with a try/catch block I can just call throw error
but I am trying to accomplish this with nested promises. Currently, it just passes the error in the lower promise catch handler to the response of the first promise and does not treat the lower error as an actual error in the outer promise.
Note: Going off what Codebling said about the unclear behavior. The first block and the second are in 2 different files and are loaded in before they are called so both are accessible. Secondly, the first promise at the top is doing stuff and then it calls a method that is a promise and it wants to handle the errors if any. In the method getDataWithoutParams()
we have it as a promise that is making an Axios request.
Note 2: Some have said that the Axios request itself is a promise, I am aware. But what I am asking is when the Axios request sends back an error we can capture that with the .catch but if this Axios call is inside a Promise already then how do we send that caught error to the catch of the upper Promise?