Try-catch didn’t catch the error thrown from .then().catch()
I'm trying to create an Auth service for my application. I have thrown the error from the .then().catch(), but it wasn’t caught by the try-catch.
async logIn(
req: Request,
res: Response,
next: NextFunction
): Promise>> {
try {
const { userName, password } = req.body;
const user = await this.userService.getUserByPassword(
userName,
password
);
if (user) {
const userData: UserData = {
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
userName: user.userName
};
this.authService
.generateAccessToken(userData, ROLE.admin)
.then((accessToken) => {
return res.cookie('token', accessToken).json(userData);
})
.catch((err) => {
// thrown from here
throw err;
});
}
} catch (err) {
const error = errorHandler(err);
return res.status(400).send(error);
}
}
Why did this happen ?