[Help] ES6/7 & Async await Best Practise

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. Also, found this site where they have a tutorial and an accompanying service plugin. I am trying it at the moment. Though I am quite unclear about the necessary steps.

Will be grateful for any directions.

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.

1 Like

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 , 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 ?

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.

2 Likes

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?

2 Likes

@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 .

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.

1 Like