Let's take this example async function, asyncFunction, which takes a Boolean parameter res. if res is true, the promise will resolve with the string "success". if res is false, the promise will reject with an error object containing the string "success".
const asyncFunction = (res) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (res) return resolve("Success");
return reject(new Error("Error"));
}, 1000);
});
};
I am calling asyncFunction from the function innerFunction when uses a try-catch block. So far so good.
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
return error;
}
};
In outerFunction, I'm executing
const outerFunction = async () => {
try {
const result = await innerFunction();
console.log(result);
} catch (error) {
console.log("caught error inside outerFunction");
console.log(error);
}
};
Finally, I'm running outer function.
outerFunction();
I expected both catch blocks to catch the error and I was wrong.
I guess that you can't catch the same exception twice? I'm not sure but my guess is that the Error object, behind the scenes, gets flagged as a "handled" error and therefore does not get recognized as one in outerFunction. (I would love some insight on this!)
My solution is to throw a new error in innerFunction:
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
throw new Error(error);
}
};
I apologize for the length, but finally, my question is: is this a "correct" way of handling a situation where I must handle the error in outerFunction? Is there any "better" way to do this?
Thank you!