To fix the UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block error with Node, we add a catch
block.
For instance, we write
router.get("/emailfetch", authCheck, async (req, res, next) => {
try {
const emailFetch = await gmaiLHelper.getEmails(
req.user._doc.profile_id,
"/messages",
req.user.accessToken
);
res.send(emailFetch.data);
} catch (err) {
next(err);
}
});
to wrap our code that might thrown an error with a try block.
Then when an error is thrown, the code in the catch block will run.