Skip to content

Commit 380c974

Browse files
committed
Refactor to async/await
1 parent 8354e8d commit 380c974

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/lambda.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@ const eventWithMultiValueHeaders = (event: handler.APIGatewayEvent): boolean =>
1818

1919
const handlerPromise = (appPromiseFn: () => Promise<RequestListener>): handler.APIGatewayEventHandler => {
2020
let _p: Promise<RequestListener> | null = null;
21-
return (event, ctx) => {
21+
return async (event, ctx) => {
2222
if (!_p) {
2323
_p = appPromiseFn();
2424
}
25-
return _p
26-
.then(app => processRequest(app, event, ctx));
27-
}
25+
const app = await _p;
26+
return processRequest(app, event, ctx);
27+
}
2828
}
2929

30-
const processRequest = (app: RequestListener, event: handler.APIGatewayEvent, ctx?: handler.LambdaContext): Promise<handler.LambdaResponse> =>
31-
Promise.resolve()
32-
.then(() => {
33-
const reqOptions = eventToRequestOptions(event, ctx);
34-
const appHandler = inProcessRequestHandler(app);
35-
return appHandler(reqOptions)
36-
})
37-
.then(res => inProcessResponseToLambdaResponse(res, eventWithMultiValueHeaders(event)))
38-
.catch(e => {
39-
console.error(e);
40-
return errorResponse();
41-
});
30+
const processRequest = async (app: RequestListener, event: handler.APIGatewayEvent, ctx?: handler.LambdaContext): Promise<handler.LambdaResponse> => {
31+
try {
32+
const reqOptions = eventToRequestOptions(event, ctx);
33+
const appHandler = inProcessRequestHandler(app);
34+
const mockResponse = await appHandler(reqOptions);
35+
return inProcessResponseToLambdaResponse(mockResponse, eventWithMultiValueHeaders(event));
36+
} catch (e) {
37+
console.error(e);
38+
return errorResponse();
39+
}
40+
}
4241

43-
const handler = (app: RequestListener): handler.APIGatewayEventHandler => handlerPromise(() => Promise.resolve(app));
42+
const handler = (app: RequestListener): handler.APIGatewayEventHandler => handlerPromise(async () => app);
4443

4544
handler.deferred = handlerPromise;
4645

0 commit comments

Comments
 (0)