I'm new to Promises and async/await programming and I am not sure I am getting it straight. I am creating an API in Nodejs, with Express, Mongoose and MongoDB. I have seen a lot of tutorials on how to deal with asynchronicity but all of them are about NodeJs projects where the routing and the DB query are in the same file. example:
const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
router.get('/users/:id', asyncMiddleware(async (req, res, next) => {
const something = await getSomethingFromDb({ id: req.params.id })
res.json(something);
}));
However, for clarity purposes, I have separated the routing from the controller but I have serious doubts I have done it correctly. Here is my code:
router.js
const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
router.get('/something/:id', asyncMiddleware(async (req, res, next) => {
const answer = await somethingController.findById(req, res, next)
}));
controller.js
exports.findById = async (req, res, next) => {
const something = await Something.findById(req.params.id).exec();
res.send(something);
};
I have tried to console.log() stuff to check what gets printed what, but I have realized, due to the awaiting part, this whole piece of code will wait for the query to finish. Is this well implemented? How can I test it?
Versions: NodeJs v10.16.3 Mongoose v5.7.1