I have the following simplified middleware function:
router.put('/', function (req, res, next) {
const data = req.body;
const q = req.parsedFilterParam;
const opts = req.parsedQueryOpts;
ResponseCtrl.update(q, data, opts)
.then(stdPromiseResp(res))
.catch(next);
});
I want to add some ability to catch errors to the middleware, just to save some code, something like this:
router.put('/', function (req, res, next) {
const data = req.body;
const q = req.parsedFilterParam;
const opts = req.parsedQueryOpts;
return ResponseCtrl.update(q, data, opts)
.then(stdPromiseResp(res));
});
so we now return the promise in the middleware function, and we can forgo the catch block.
so internally, it might look like this now:
nextMiddlewareFn(req,res,next);
just looking to change it to:
const v = nextMiddlewareFn(req,res,next);
if(v && typeof v.catch === 'function'){
v.catch(next);
});
does anyone know how to do this with Express?