# \[Help\] ES6/7 & Async await Best Practise

**URL:** <https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124>\
**Category:** Serverless Framework\
**Created:** [November 16, 2017, 4:53am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124 "2017-11-16T04:53:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaiyes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kaiyes/32/1278_2.png) [@kaiyes](https://forum.serverless.com/u/kaiyes)\
**Post date:** [November 16, 2017, 4:53am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/1 "2017-11-16T04:53:06Z")

</div>

Hi guys. I am graphQL dev working on integration with a serverless App. I want to use `async-await` and `import` in the server. Looks like lambda uses an older version of node.js, hence we gotta use some sort of babel runtime and/or webpack config. I wanted to know what are the best practises.

I have found [this thread](http://forum.serverless.com/t/best-way-to-get-es7-async-await-and-import-export-syntax-working/1412). Also, found [this site](https://serverless-stack.com/chapters/serverless-es7-service.html) where they have a tutorial and an [accompanying service plugin](https://github.com/AnomalyInnovations/serverless-es7). I am trying it at the moment. Though I am quite unclear about the necessary steps.

Will be grateful for any directions.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [November 16, 2017, 6:45am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/2 "2017-11-16T06:45:22Z")

</div>

Lambda uses Node 6 which doesn’t support async/await. You need to use something like Babel or TypeScript (my preference) that can transpile newer versions of ES to run on older versions of Node. Using Webpack is one way to achieve this. The serverless-webpack plugin will use Webpack to build the upload file which allows you to run Babel or TypeScript as part of the build process.

Personally I’m a little old school on this (pre serverless-webpack) using npm to handle deployments. My deployment script is setup to lint and compile TypeScript (creating a version that will run on Node 6) then running tests before doing the deployment.

If I was starting from scratch today I’d probably look at serverless-webpack first.

---

<div class="post-metadata">

**Author:** ![kaiyes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kaiyes/32/1278_2.png) [@kaiyes](https://forum.serverless.com/u/kaiyes)\
**Post date:** [November 16, 2017, 6:56am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/3 "2017-11-16T06:56:07Z")

</div>

thanks a lot @buggy. We use cirle ci for managing deployments. But more manual is probably useful in this scenario

What I don’t understand is, according to the [serverless-stack tutorial](https://serverless-stack.com/chapters/add-support-for-es6-es7-javascript.html) , webpack is being used as dev-dependency only. So if, by using `babel-preset-es2015`, I can use imports locally, why can’t I run it in lambda when `babel-runtime` is provided as dependency ?

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [November 16, 2017, 8:24am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/4 "2017-11-16T08:24:45Z")

</div>

Webpack is a build tool so you don’t need it at runtime (in Lambda).

With TypeScript (I believe Babel works similarly): Serverless uses the serverless-webpack plugin to run Webpack which builds the files that are uploaded to Lambda. Webpack uses TypeScript (or Babel) to compile ES7/8 code back to ES6 that’s supported by Node 6. It’s the compiled ES6 version that is being sent to Lambda and not your ES 7/8 source versions.

From what I’ve read the bable-runtime package add polyfills for missing objects and functions but it won’t help with missing syntax. That’s what the babel compiler run by Webpack handles.

---

<div class="post-metadata">

**Author:** ![reecefenwick](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/reecefenwick/32/1318_2.png) [@reecefenwick](https://forum.serverless.com/u/reecefenwick)\
**Post date:** [November 22, 2017, 1:56pm UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/5 "2017-11-22T13:56:30Z")

</div>

I actually prefer to use syntax that is native to the NodeJS runtime Lambda supports.

This is simply because debugging transpiled code is near impossible, at least in my experience.

NodeJS version 8 is the latest LTS version (supports async/await natively) which I imagine is somewhere in Amazon’s roadmap to support.

I’d recommend holding out for that, unless someone has had a different experience with transpiled code in Lambda?

---

<div class="post-metadata">

**Author:** ![kaiyes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kaiyes/32/1278_2.png) [@kaiyes](https://forum.serverless.com/u/kaiyes)\
**Post date:** [November 23, 2017, 1:04am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/6 "2017-11-23T01:04:38Z")

</div>

@reecefenwick you read my mind. I was thinking the same thing. Since I came from express, I am used to using async-await. That’s why ! But I guess I could wait .

---

<div class="post-metadata">

**Author:** ![bni](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bni/32/1329_2.png) [@bni](https://forum.serverless.com/u/bni)\
**Post date:** [November 25, 2017, 11:20am UTC](https://forum.serverless.com/t/help-es6-7-async-await-best-practise/3124/7 "2017-11-25T11:20:46Z")

</div>

You could use coroutine and generator function, with yield in the mean time.

The syntax is a bit dense, but code like that should be very easy to change over to async/await later when Lambda supports a newer runtime.
