Skip to content

Commit 42d3c35

Browse files
author
swyx
committed
add async example
1 parent f4976e1 commit 42d3c35

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ This project is based on [Create React App v2](https://github.com/facebookincuba
22

33
The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment.
44

5-
As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`.
5+
As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. We've also included an async lambda example using async/await syntax in `async-chuck-norris.js`.
66

77
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda)
88

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.4.0",
44
"private": true,
55
"dependencies": {
6+
"node-fetch": "^2.3.0",
67
"react": "^16.6.3",
78
"react-dom": "^16.6.3",
89
"react-scripts": "^2.1.1"

src/App.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class LambdaDemo extends Component {
88
this.state = { loading: false, msg: null };
99
}
1010

11-
handleClick = e => {
11+
handleClick = api => e => {
1212
e.preventDefault();
1313

1414
this.setState({ loading: true });
15-
fetch('/.netlify/functions/hello')
15+
fetch('/.netlify/functions/' + api)
1616
.then(response => response.json())
1717
.then(json => this.setState({ loading: false, msg: json.msg }));
1818
};
@@ -22,9 +22,12 @@ class LambdaDemo extends Component {
2222

2323
return (
2424
<p>
25-
<button onClick={this.handleClick}>
25+
<button onClick={this.handleClick('hello')}>
2626
{loading ? 'Loading...' : 'Call Lambda'}
2727
</button>
28+
<button onClick={this.handleClick('async-chuck-norris')}>
29+
{loading ? 'Loading...' : 'Call Async Lambda'}
30+
</button>
2831
<br />
2932
<span>{msg}</span>
3033
</p>

src/lambda/async-chuck-norris.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// example of async handler using async-await
2+
// https://github.com/netlify/netlify-lambda/issues/43#issuecomment-444618311
3+
4+
import fetch from 'node-fetch';
5+
export async function handler(event, context) {
6+
try {
7+
const response = await fetch('https://api.chucknorris.io/jokes/random');
8+
if (!response.ok) {
9+
// NOT res.status >= 200 && res.status < 300
10+
return { statusCode: response.status, body: response.statusText };
11+
}
12+
const data = await response.json();
13+
14+
return {
15+
statusCode: 200,
16+
body: JSON.stringify({ msg: data.value })
17+
};
18+
} catch (err) {
19+
console.log(err); // output to netlify function log
20+
return {
21+
statusCode: 500,
22+
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err)
23+
};
24+
}
25+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6046,6 +6046,11 @@ no-case@^2.2.0:
60466046
dependencies:
60476047
lower-case "^1.1.1"
60486048

6049+
node-fetch@^2.3.0:
6050+
version "2.3.0"
6051+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
6052+
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==
6053+
60496054
node-forge@0.7.5:
60506055
version "0.7.5"
60516056
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.5.tgz#6c152c345ce11c52f465c2abd957e8639cd674df"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy