I'm having some issues understanding why I'm getting the following results:
I have this function in UserService.js
async function addUser(username, divisionId, points){
const newUser=new User({username: username, divisionId: divisionId, points:points});
await newUser.save();//save is a function from mongoose which returns a promise
}
And this code in userController.js
router.route('/addUser').post((req, res) => {
const username = req.body.username;
const divisionId = req.body.divisionId;
const points = 0;
userService.addUser(username,divisionId, points)
.then(() => res.json('User added!'))
.catch((err=>res.status(400).json('Error '+ err)));
});
I have realized that if I remove the await in: await newUser.save()
the exceptions wont be caught in the userController. I just can't seem to understand why; If .save() throws an exception shouldn't it propagate to addUser and be caught in the catch block?
Should I keep the await? Isn't it bad to be waiting for something just for this purpose in an asynchronous context?
Sorry if this is a dumb question but I couldn't find anything that specifically addresses this propagation of exceptions in async contexts