Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs 18: Top Level Await using fetch API #13

Open
djsd123 opened this issue Aug 3, 2023 · 0 comments
Open

Nodejs 18: Top Level Await using fetch API #13

djsd123 opened this issue Aug 3, 2023 · 0 comments

Comments

@djsd123
Copy link

djsd123 commented Aug 3, 2023

Hi there,

According to this blog-post top-level awaits should be fully supported. However, when I try this with the fetch API, I still have to use the await keyword within my handler function otherwise response.text() will be empty.

In short:
message: await response.text(),

vs

message: response.text(),

Am I misunderstanding something?

import { APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyResult} from 'aws-lambda'

const url = 'https://aws.amazon.com/'
// fetch API method is available from Nodejs 18
const response = await fetch(url)

export const handler: APIGatewayProxyHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    try {
        return {
            statusCode: response.status,
            body: JSON.stringify({
                message: await response.text(),    // <- HERE
                // input: event,
            }),
        }
    } catch (error) {
        console.log(error)
        return {
            statusCode: 500,
            body: JSON.stringify({message: error.body})
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant