Async / Await in Node.js v7

Tyler Smith
3 min readNov 11, 2016

--

Late last year, the async and await keywords were introduced to the ECMAScript specification. Until recently, however, they were only really available if you were using a language that transpiled to JavaScript, such as TypeScript, and even then you have to target ES6 to use them.

With the release of Node.js version 7, we can now use asynchronous functions natively on the back end.

How It Works

The process of handling asynchronous functions in JavaScript has changed a lot over recent years. We started with the ugly world of callbacks, then we were introduced to Promises and Generators.

Now we have the async and await keywords available, so I will show you how a basic Mongoose query looks with these keywords, compared to existing methods.

For my example query, I will be doing a simple update operation for a To-Do list API. Here is the query using callbacks:

Update a task using callbacks

This doesn’t look too bad, but when you start making a complex application with several asynchronous function, you start to get into callback hell.

Let’s take a look at the same function, using Promises:

Update a task using Promises

As you can see, things didn’t really improve from that, but Promises become a huge improvement when working with complex chains. Instead of nesting deeper and deeper with callbacks, you can chain then calls to keep at a sane level of nesting.

Even with all those improvements, complex applications can still start to look pretty ugly when you have a lot of those then calls. Enter async and await.

Here is the same function as the two above, using async and await:

Update a task using async and await

You may have noticed that the code actually got a line longer with each method, but it is not all about making your code shorter. Even though the function got longer, it looks much more structured now and, with constructs like try and catch, it will be much more familiar to programmers coming from other languages.

Note: I have posted a full sample to-do list API on Github.

The Syntax

Now that we have seen a quick example, let’s break down the syntax that was used.

Inside the function, we have the await keyword. You can put this in front of any function that returns a Promise to wait for the function to finish, and have it return its. This is the value that you would usually capture in your then function.

This process, however, loses the error that could also usually be captured in a then. To catch any error, we surround the line of code that contains the await with a try...catch clause.

Now that we have added await to our function, we must make that function asynchronous by adding the async keyword in front of the declaration. This makes the function return a Promise implicitly. You can use the keyword with an arrow function, as I did in the example, or you can also use it with an anonymous function:

exports.update = async function(req, res) {
}

Or you can use it with a named function declaration:

async function updateTask() {
}

Can I Use It Without Node v7?

These are a couple other places, besides Node version 7, that the async and await keywords are supported:

  • Babel: If you use Babel to transpile your JavaScript into older version, it has full support now for async and await.
  • TypeScript: Versions 1.7 and up support async and await, but only if you are targeting ES6. So you can really only use it to target older versions of Node or other non-browser uses, such as React Native; however, they plan to add support for ES5 as a target in TypeScript version 2.1.0.
  • Current Browser Support: Chrome has added support in version 55, which is in beta at the time of writing; Firefox added support in version 52, which is in their nightly channel; and Opera added support in version 42, which is in their developer channel.

--

--

Tyler Smith

I am a full stack developer who specializes in web and mobile applications.