Async support

Now awaits function
This commit is contained in:
nicco 2017-08-22 12:46:50 +02:00
parent f134a5814e
commit 79f7daf0b5

View File

@ -11,7 +11,9 @@ const
METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'], METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ALL'],
// Default Response // Default Response
defaultResponse = [() => {}, {}] defaultResponse = [async(c, n) => {
await n()
}, {}]
/** /**
* Defaults an options object * Defaults an options object
@ -181,7 +183,7 @@ module.exports = function (options, builder) {
// Build the routes // Build the routes
const routes = mkRouter(options, builder) const routes = mkRouter(options, builder)
return function (c, n) { return async function (c, n) {
// For building nested routes // For building nested routes
if (typeof c === 'string') { if (typeof c === 'string') {
options.prefix = c + options.prefix options.prefix = c + options.prefix
@ -190,9 +192,8 @@ module.exports = function (options, builder) {
const fn = chooseFn(routes, c.request.url, c.request.method) const fn = chooseFn(routes, c.request.url, c.request.method)
c.request.params = fn[1] c.request.params = fn[1]
if (typeof fn[0] === 'function') if (typeof fn[0] !== 'function')
fn[0](c, n) fn[0] = defaultResponse[0]
else await fn[0](c, n)
defaultResponse[0]()
} }
} }