File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed Expand file tree Collapse file tree 2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -22,14 +22,16 @@ class LambdaDemo extends Component {
22
22
23
23
return (
24
24
< p >
25
+ < span > { msg } </ span > < br />
25
26
< button onClick = { this . handleClick ( 'hello' ) } >
26
27
{ loading ? 'Loading...' : 'Call Lambda' }
27
- </ button >
28
+ </ button > < br />
28
29
< button onClick = { this . handleClick ( 'async-chuck-norris' ) } >
29
- { loading ? 'Loading...' : 'Call Async Lambda' }
30
+ { loading ? 'Loading...' : 'Call Lambda - Async' }
31
+ </ button > < br />
32
+ < button onClick = { this . handleClick ( 'promise-chuck-norris' ) } >
33
+ { loading ? 'Loading...' : 'Call Lambda - Promise' }
30
34
</ button >
31
- < br />
32
- < span > { msg } </ span >
33
35
</ p >
34
36
) ;
35
37
}
Original file line number Diff line number Diff line change
1
+ // example to have the handler return a promise directly
2
+ // https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/
3
+
4
+ import fetch from 'node-fetch' ;
5
+ export async function handler ( event , context ) {
6
+ return new Promise ( ( resolve , reject ) => {
7
+ fetch ( 'https://api.chucknorris.io/jokes/random' )
8
+ . then ( response => {
9
+ if ( response . ok ) { // response.status >= 200 && response.status < 300
10
+ return response . json ( ) ;
11
+ } else {
12
+ resolve ( { statusCode : response . status , body : response . statusText } )
13
+ } ;
14
+ } )
15
+ . then ( data => {
16
+ const response = {
17
+ statusCode : 200 ,
18
+ headers : { 'content-type' : 'application/json' } ,
19
+ body : JSON . stringify ( { msg : data . value } )
20
+ }
21
+ resolve ( response ) ;
22
+ } )
23
+ . catch ( err => {
24
+ console . log ( err )
25
+ resolve ( { statusCode : 500 , body : err . message } ) ;
26
+ } )
27
+ } ) ;
28
+ }
You can’t perform that action at this time.
0 commit comments