# Async/await lambda function example

**URL:** <https://answers.netlify.com/t/async-await-lambda-function-example/6976>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [January 6, 2020, 5:56am UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976 "2020-01-06T05:56:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshkmartinez](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/joshkmartinez/32/5209_2.png) [@joshkmartinez](https://answers.netlify.com/u/joshkmartinez)\
**Post date:** [January 6, 2020, 5:56am UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/1 "2020-01-06T05:56:05Z")

</div>

I was taking a look at the lambda function examples in the [netlify-lambda github repo](https://github.com/netlify/netlify-lambda#usage) (you’ll have to scroll down a bit to see what I am referencing) and there seem to be two styles, the traditional callback style:

```auto
exports.handler = function(event, context, callback) {
// your server-side functionality
callback(null, {
  statusCode: 200,
  body: JSON.stringify({
    message: `Hello world ${Math.floor(Math.random() * 10)}`
  })
});
};

```

and the async/await style:

```auto
export async function handler(event, context) {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello world ${Math.floor(Math.random() * 10)}` })
  };
}

```

I tried out the traditional style and it works fine for me, but when I do the async/await style I get the error `Function invocation failed: SyntaxError: Unexpected token 'export'`.  
Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![jmcmullen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jmcmullen/32/5551_2.png) [@jmcmullen](https://answers.netlify.com/u/jmcmullen)\
**Post date:** [January 8, 2020, 3:28pm UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/2 "2020-01-08T15:28:07Z")

</div>

I encountered the same problem, managed to get async/await working like this:

```auto
exports.handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello world ${Math.floor(Math.random() * 10)}` })
  };
}

```

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [January 9, 2020, 5:58pm UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/3 "2020-01-09T17:58:02Z")

</div>

thanks for sharing your solution - @joshkmartinez, does this work for you?

---

<div class="post-metadata">

**Author:** ![joshkmartinez](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/joshkmartinez/32/5209_2.png) [@joshkmartinez](https://answers.netlify.com/u/joshkmartinez)\
**Post date:** [January 10, 2020, 3:15am UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/4 "2020-01-10T03:15:25Z")

</div>

Yes, his way works, I’ve been doing it in a similar way:

```auto
handler = async (event, context) => {
  return {
    statusCode: 200,
    body: "pong"
  };
};
exports.handler = handler;

```

The main thing was though if this example doesn’t work, why is it listed as one? I was really wondering if it was something I did wrong and it seems like the problem is with the example?

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [January 10, 2020, 5:39pm UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/5 "2020-01-10T17:39:12Z")

</div>

@joshkmartinez - firstly, glad it is working now. Secondly, that’s a valid question - as to why the example seems to not work for you. I’ll do some digging.

---

<div class="post-metadata">

**Author:** ![swyx](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/swyx/32/269_2.png) [@swyx](https://answers.netlify.com/u/swyx)\
**Post date:** [January 10, 2020, 5:52pm UTC](https://answers.netlify.com/t/async-await-lambda-function-example/6976/6 "2020-01-10T17:52:11Z")

</div>

hi folks! the example works fine - IF you use `netlify-lambda`! that’s why it’s in the `netlify-lambda` repo. If you are NOT using `netlify-lambda`, and use the `export` syntax, you will get this exact error. If you want more function examples without `netlify-lambda` you can check [the docs](https://docs.netlify.com/functions/build-with-javascript/#format), or [Functions examples - Netlify Functions](https://functions.netlify.com/examples/) or [https://functions-playground.netlify.com/](https://functions-playground.netlify.com/)

One of the reasons `netlify-lambda` exists is because Node.js does not yet have ES modules support. ES modules syntax is the `import` and `export` syntax that you see me writing in there (note that this is different from the valid `exports.functionName` syntax above - confusing, i know). One of the express purposes of `netlify-lambda` is to allow people to write ES modules syntax, because it uses webpack underneath with a node target - hence copying an example directly from its readme without using the tool won’t work in “raw Node”.

Hope that makes sense and i’ll modify the readme to make clear.
