Skip to content

Commit

Permalink
improvement: Micro-optimize the runHandler() function
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Jun 29, 2019
1 parent b6c36e4 commit 2017e02
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/RequestHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ function runOnFinishedHooks() {
}

function runHandler(res) {
var result = res._route.handler(res.request, res)
const result = res._route.handler(res.request, res)

if (result && typeof result.then === 'function') {
result
.then((payload) => {
if (payload !== undefined) {
res.send(payload)
}
}, (err) => {
if (res.sent) {
throw err // Re-throw the error since it is a system error
}
runOnErrorHooks(err, res)
})
result.then(onHandlerResolve.bind(res), onHandlerReject.bind(res))
}
}

function onHandlerResolve(body) {
if (body !== undefined) {
this.send(body)
}
}

function onHandlerReject(err) {
if (this.sent) {
throw err // Re-throw the error since it is a system error
}
runOnErrorHooks(err, this)
}

function createOptionsHandler(allowedMethods) {
Expand Down

0 comments on commit 2017e02

Please sign in to comment.