Skip to content

Commit

Permalink
Avoid async/await for run()
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Feb 10, 2017
1 parent 4f0415f commit a4cf235
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const serve = fn => server((req, res) => {

module.exports = exports = serve

exports.run = run
exports.json = json
exports.send = send
exports.sendError = sendError
Expand All @@ -39,6 +38,22 @@ async function run(req, res, fn) {
}
}

exports.run = (req, res, fn) => {
fn(req, res).then(val => {
// Return 204 No Content if value is null
if (val === null) {
send(res, 204, null)
}

// Return a undefined-null value -> send
if (undefined !== val) {
send(res, res.statusCode || 200, val)
}
}).catch(err => {
sendError(req, res, err)
})
}

// maps requests to buffered raw bodies so that
// multiple calls to `json` work as expected
const rawBodyMap = new WeakMap()
Expand Down

0 comments on commit a4cf235

Please sign in to comment.