Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.

Commit 007ff93

Browse files
chore: use async syntax instead of callbacks (#80)
* chore: use async/await instead of callbacks
1 parent f443647 commit 007ff93

File tree

9 files changed

+17
-58
lines changed

9 files changed

+17
-58
lines changed

.prettierrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
singleQuote: true,
3-
trailingComma: "es5"
2+
"singleQuote": true,
3+
"trailingComma": "es5"
44
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Ignoring the scheduling event, you can see here that we're setting up a function
4848

4949
#### 2. Create your function
5050

51-
This starter kit's Hello World function (which you will of course get rid of) can be found at [`./src/hello.js`](./src/hello.js). There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible). Like most Serverless functions, the `hello` function accepts an event, context, and callback. When your function is completed, you execute the callback with your response. (This is all basic Serverless; if you've never used it, be sure to read through [their docs](https://serverless.com/framework/docs/).
51+
This starter kit's Hello World function (which you will of course get rid of) can be found at [`./src/hello.js`](./src/hello.js). There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible). Like most Serverless functions, the `hello` function is asynchronous and accepts an event & context. (This is all basic Serverless; if you've never used it, be sure to read through [their docs](https://serverless.com/framework/docs/).
5252

5353
---
5454

src/__snapshots__/hello.test.js.snap

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`hello executes as expected 1`] = `
4-
[MockFunction] {
5-
"calls": Array [
6-
Array [
7-
null,
8-
Object {
9-
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{}}",
10-
"statusCode": 200,
11-
},
12-
],
13-
],
14-
"results": Array [
15-
Object {
16-
"type": "return",
17-
"value": undefined,
18-
},
19-
],
4+
Object {
5+
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{}}",
6+
"statusCode": 200,
207
}
218
`;

src/hello-ts.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { successResponse, runWarm } from './utils';
22

3-
const helloTs: AWSLambda.Handler = (
4-
event: AWSLambda.APIGatewayEvent,
5-
_context,
6-
callback
7-
) => {
3+
const helloTs: Function = async (event: AWSLambda.APIGatewayEvent) => {
84
// successResponse handles wrapping the response in an API Gateway friendly
95
// format (see other responses, including CORS, in `./utils/lambda-response.js)
106
const response = successResponse({
117
message: 'Go Serverless! Your function executed successfully!',
128
input: event,
139
});
1410

15-
callback(null, response);
16-
17-
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
18-
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
11+
return response;
1912
};
2013

2114
// runWarm function handles pings from the scheduler so you don't

src/hello.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { successResponse, runWarm } from './utils';
22

3-
const hello = (event, context, callback) => {
3+
const hello = async event => {
44
// successResponse handles wrapping the response in an API Gateway friendly
55
// format (see other responses, including CORS, in `./utils/lambda-response.js)
66
const response = successResponse({
77
message: 'Go Serverless! Your function executed successfully!',
88
input: event,
99
});
1010

11-
callback(null, response);
12-
13-
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
14-
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
11+
return response;
1512
};
1613

1714
// runWarm function handles pings from the scheduler so you don't

src/hello.test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import hello from './hello';
22

33
describe('hello', () => {
4-
it('executes as expected', () => {
5-
const cb = jest.fn();
6-
hello({}, {}, cb);
7-
expect(cb).toBeCalled();
8-
expect(cb).toMatchSnapshot();
4+
it('executes as expected', async () => {
5+
const response = await hello({});
6+
expect(response).toMatchSnapshot();
97
});
108
});

src/types/index.ts

Whitespace-only changes.

src/types/lambda.ts

-15
This file was deleted.

src/utils/run-warm.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const runWarm = (lambdaFunc: AWSLambda.Handler): AWSLambda.Handler => (
1+
const runWarm = (lambdaFunc: Function): AWSLambda.Handler => async (
22
event,
3-
context,
4-
callback
3+
context
54
) => {
65
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
76
// lambda function running hot.
87
if (event.source === 'serverless-plugin-warmup') {
9-
return callback(null, 'pinged');
8+
return 'pinged';
109
}
1110

12-
return lambdaFunc(event, context, callback);
11+
return lambdaFunc(event, context);
1312
};
1413

1514
export default runWarm;

0 commit comments

Comments
 (0)