I would like to try and convert this function into a Promise
based function in order to resolve all of these nested callbacks and return
warnings from ESLint consistent-return.
Previously, I asked for some help in overcoming an ESLint error with my return statements here as they are not consistent or following the best practices of JS.
My first thought was to simply do return new Promise((resolve, reject) => {...})
inside the remove
function, but that would just promisify the whole thing instead of just what's inside the function so I feel like that's not the best way to do this.
Any help appreciated!
function remove(req, res) {
User.findOne({ username: req.params.username }, (findErr, existingUser) => {
if (findErr) return res.status(500).send(errorHandler.getErrorMessage(findErr));
if (!existingUser) return res.status(404).send({ message: 'User not found' });
existingUser.remove((removeErr) => {
if (removeErr) return res.status(500).send(errorHandler.getErrorMessage(removeErr));
return res.json({ message: `${existingUser.username} successfully deleted` });
});
});
}