I am using async await in my asynchronous code and want to bubble up the error to the asynchronous function which calls another asynchronous function inside it. I tried this and it worked but is it better if I returned Promise.reject(new Error("hi failed")) inside catch block iniside hi() or is there a better way to do this?
async function hi() {
try {
console.log("In hi");
if (Math.random() < 0.5) throw new Error('error hi');
else return Promise.resolve('hi resolved');
} catch (error) {
console.log("In hi error");
throw error;
} finally {
console.log('hi finally');
}
}
async function hey() {
try {
console.log("In hey");
var data = await hi();
console.log(data);
} catch (error) {
console.log("In hey error");
console.log(error.message);
}
}
hey();