Common Node8 mistakes in Lambda
Tags: node, aws, javascript, serverless
Published: 2022-04-29 17:55:56

Originally posted at [Serverless](https://www.serverless.com/blog/common-node8-mistakes-in-lambda/) on October 4th, 2018

![](https://cdn-images-1.medium.com/max/2400/0*cKZN7QTHxzGrg7kv.jpeg)

It’s been 6 months since [AWS Lambda](https://serverless.com/aws-lambda/) added support Node.js 8.10. I’m super happy that I can finally use async/await to [simplify my Lambda functions](https://serverless.com/blog/aws-lambda-node-8-support-what-changes-serverless-developers/).

In the meantime, I have helped a few clients with their Node8 serverless projects. In doing so I have seen some recurring mistakes around async/await.

### Still using callbacks

Many people are still using the callbacks in their async handler functions:
{% gist https://gist.github.com/richardgrantserverless/3487089f52c5090cf53730496755756f.js %}
instead of the simpler alternative:
{% gist https://gist.github.com/richardgrantserverless/943e39046dd7aa31325baf91f23b42d9.js %}
### Not using promisify

Before Node8, [bluebird](http://bluebirdjs.com/docs/getting-started.html) filled a massive gap. It provided the utility to convert callback-based functions to promise-based. But Node8’s built-in util module has filled that gap with the promisify function.

For example, we can now transform the readFile function from the fs module like this:
{% gist https://gist.github.com/richardgrantserverless/be76caf1fb3313cd882b376bb217fdf4.js %}
No need to use bluebird anymore. That’s one less dependency, which helps reduce the cold start time for our functions.

### Too sequential

async/await lets you write asynchronous code as if they're synchronous, which is awesome. No more dealing with callback hell!

On the flip side, we can also miss a trick and not perform tasks concurrently where appropriate.

Take the following code as example:
{% gist https://gist.github.com/richardgrantserverless/a8b5262e05ee8fbb8f66fd727f7f18ea.js %}
This function is easy to follow, but it’s hardly optimal. teamModel.fetch doesn't depend on the result of fixtureModel.fetchAll, so they should run concurrently.

Here is how you can improve it:
{% gist https://gist.github.com/richardgrantserverless/fd291ec8aa68cbc8c64e7928ffec8cee.js %}
In this version, both fixtureModel.fetchAll and teamModel.fetch are started concurrently.

You also need to watch out when using map with async/await. The following will call teamModel.fetch one after another:
{% gist https://gist.github.com/richardgrantserverless/281deeb069be89c3b6159f3a035984de.js %}
Instead, you should write it as the following:
{% gist https://gist.github.com/richardgrantserverless/28e1baaa0c357091dbf0b15e8d86bb65.js %}
In this version we map teamIds to an array of Promise. We can then use [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) to turn this array into a single Promise that returns an array of teams.

In this case, teamModel.fetch is called concurrently and can significantly improve execution time.

### async/await inside forEach()

This is a tricky one, and can sometimes catch out even experienced Node.js developers.

The problem is that code like this doesn’t behave the way you’d expect it to:
{% gist https://gist.github.com/richardgrantserverless/cbb209577d0138b1574538b57a905d70.js %}
When you run this you’ll get the following output:
{% gist https://gist.github.com/richardgrantserverless/4c8c5b4a43673807fa2cab96b51b0322.js %}
See [this post](https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404) for a longer explanation about why this doesn’t work. For now, just remember to **avoid using async/await inside a forEach**!

### Not using AWSSDK’s .promise()

Did you know that the AWS SDK clients support both callbacks and promises? To use async/await with the AWS SDK, add .promise() to client methods like this:
{% gist https://gist.github.com/richardgrantserverless/a26870a1695befc5f80d8c88045dbebb.js %}
No more callback functions, yay!

### Wrap-up

That’s it, 5 common mistakes to avoid when working with Node.js 8.10 in Lambda. For more tips on building production-ready serverless applications and operational best practices, check out my [video course](https://bit.ly/production-ready-serverless). ;-)

Further reading:

* [AWS Lambda Node8 support: what it changes for serverless developers](https://serverless.com/blog/aws-lambda-node-8-support-what-changes-serverless-developers/)

![](https://cdn-images-1.medium.com/max/2000/1*H6qj1ymTEs5Wp9SAdhVAWw.png)

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/common-node8-mistakes-in-lambda/).*