Skip to content

Instantly share code, notes, and snippets.

@dabroder
Created April 16, 2019 16:40
Show Gist options
  • Save dabroder/3db7b6d8865cb1fb0cf0fb219f6fa57b to your computer and use it in GitHub Desktop.
Save dabroder/3db7b6d8865cb1fb0cf0fb219f6fa57b to your computer and use it in GitHub Desktop.
Small promises wrappers
const httpError = require('http-errors')
exports.wrap = (f, status = 200) => (req, res, next) => {
Promise.resolve(req).then(f).then(r => {
if (status === 204) res.status(204).end()
else res.status(status).json(r)
}).catch(next)
}
exports.httpError = httpError
exports.hError = (status, msg, props) => Promise.reject(httpError(status, msg, props))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment