I am currently stuck on understanding how asyncHandler really works. I understand that asyncHandler is a wrapper method for controller methods so that we don't have to write try/catch on every method that we call on controller or services/repo (that is a good dry). I also understand that async/await returns promises so
return Promise.resolve(fn(req, res, next)).catch(next);
is acting as a try/catch. below is the full script of asynchandler
var asyncHandler = function (fn) {
return function (req, res, next) {
return Promise.resolve(fn(req, res, next)).catch(next);
};
};
module.exports = asyncHandler;
what I dont understand is how
return function(req,res,next)
is working?