I am trying to loop over an array using forEach
loop with async
callback in javascript. The problem is that when I throw an exception it gives
(node:2500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
Although I have a catch block after forEach
. Here is my code
async function func() {
try {
array.forEach(async (e) => {
try {
let bom = await BOMChild.findOne({
where: { id: e.id },
});
if (bom.BOMProductId || bom.BOMProductId === 0) {
throw {
name: " error",
description: `Description `,
};
}
} catch (e) {
throw e;
}
});
} catch (e) {
//exception caught
}
}
What I am doing wrong and what is the possible way to solve this. TIA