Created
April 16, 2019 16:40
-
-
Save dabroder/3db7b6d8865cb1fb0cf0fb219f6fa57b to your computer and use it in GitHub Desktop.
Small promises wrappers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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