support non-native promise implementations

This commit is contained in:
Rich Harris
2018-10-24 21:05:25 -04:00
parent 65128118c7
commit 4991f3b359

View File

@@ -2,7 +2,7 @@ import { IGNORE } from '../placeholders';
import { Req, Res, ServerRoute } from './types'; import { Req, Res, ServerRoute } from './types';
export function get_server_route_handler(routes: ServerRoute[]) { export function get_server_route_handler(routes: ServerRoute[]) {
function handle_route(route: ServerRoute, req: Req, res: Res, next: () => void) { async function handle_route(route: ServerRoute, req: Req, res: Res, next: () => void) {
req.params = route.params(route.pattern.exec(req.path)); req.params = route.params(route.pattern.exec(req.path));
const method = req.method.toLowerCase(); const method = req.method.toLowerCase();
@@ -53,12 +53,7 @@ export function get_server_route_handler(routes: ServerRoute[]) {
}; };
try { try {
const result = handle_method(req, res, handle_next); await handle_method(req, res, handle_next);
// catch failures in async functions
if (Promise.resolve(result) === result) {
result.catch(handle_next);
}
} catch (err) { } catch (err) {
handle_next(err); handle_next(err);
} }