I have an async function which return promise
In this promise I have scope function and inside that I have 2 other scope functions
If I try to throw error on function a the code continues it`s execution
async someAsyncFunc(): Promise<void> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let port: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let writer: any;
const test = async () => {
const a = async () => {
throw new Error();
};
const b = async () => {
// do some work
await a();
};
try {
await b();
} catch (err) {
console.log(err);
}
resolve();
};
try {
test().catch((err) => {
console.log("catch block error");
console.error(err);
reject(err);
});
} catch (error) {
return reject(error);
} finally {
}
});
}
I want somehow to handle all errors that could happen on either functions a,b and load at this final try catch block of the returned promise
try {
test().catch((err) => {
console.log("catch block error");
console.error(err);
reject(err);
});
} catch (error) {
return reject(error);
} finally {
}
try { await b(); } catch (err) { console.log(err); }
You're catching and handling the error there.
You can't get it in test()'s catch handler because no error is raised to that point.
It's already been handled.
You'd need to throw it again if you want something else to catch it:
try {
await b();
} catch (err) {
console.log(err);
throw err;
}