When I have:
try {
array.forEach(items => {
//do something
//here something is going wrong
})
res.status(200).json({
success: true
})
} catch (err) {
res.status(400).json({
message: err.message
})
}
Error is not catching - I've got 200 response but I can see crashed app and on terminal I have error. But this error is not catched by catch(err) block.
But if I have For of loop I'm getting 400 response.
Erros is the same - in forEach it is not catched and response is ok but in for of loop erros is catched and I have 400 response with error. Why? Why I can't catch error in forEach and send 400 res by catch block?