promise().catch(next)
async function, try ~ catch, next
const asyncHandler = (requestHandler) => {
return async (req, res, next) => {
try{
await requestHandler(req, res);
} catch (err) {
next(err);
}
}
}
router.get('/', asyncHandler(async (req, res) => {
const posts = await Posts.find({});
if(posts.length < 1){
throw new Error("Not Found");
}
res.render('posts/list', { posts } );
});