The Best Feature in JavaScript just came to Node.JS — Async / Await

The best feature in JavaScript has finally come natively to Node.JS, the greatest of them all — Async / Await. With the addition of Async / Await in the latest release of Google Chrome and it’s V8 engine it is now been standardised in the latest release of Node.JS 7.10.0 which is great news for JavaScript and Node Developers.

But what exactly is Async Await?

Don’t worry, I can explain. To make some sense of it, we will go through the typical example of “I have to make two calls to an API but the second query will rely on the first query’s results”. In our example both snippets of code will first search GitHub for the username “jamesmarino” then it will query the GitHub profile API to get the blog URL of this queried username.

Firstly, Callbacks

First, let’s go through this example using the traditional “callback” approach, displaying the infamous callback hell in the below code in the getProfileBlog(usernameToSearch) function.

We can see that the getProfileBlog(usernameToSearch) function is long, untidy, heavily nested and has duplicate error handlers. Watch as we get this cleaned up.

Introducing Async /Await

The below code does the exact same as the above, but uses async await. Can you see the differences?

As you can see the getProfileBlog(usernameToSearch) function has been cut down in size to just 9 lines compared to the callback approach of 18 lines. This is a 50% reduction in code not to mention the code being cleaner, better structured and more readable.

A more technical explanation

1. The request(url) function

In this function we return a promise, which is a “value which may be available now, or in the future, or never”. In other words returning a promise tells the program that you are trying to do something that is asynchronous (such as requesting some data from a server), and you will eventually get back to the program with some data.

The promise object takes a function as a parameter with two function pointers, resolve and reject . resolve() is treated like a callback for the return of successful data and reject() is also treated like a callback but for errors and will trigger a try / catch block just as throwing an exception would (while using the async await approach). You can also use the functionWillRetrunPromise().then((data) => {}).catch((error) => {}) approach if you are not using Async Await.

2. The getProfileBlog(usernameToSearch) function

This function will actually use the async await approach. Declaring the function as async getProfileBlog() tells us that it will still run asynchronously and that it is “non blocking” as said in the JavaScript world. It also allows us to use await within this function. Note we could also write this as async function getProfileBlog() , it does not have to be in a JavaScript class.

The next part is the await . As seen in the const searchData = await GitHub.request(searchURL) line we are telling the async function to wait until the promise returned from the request(searchURL) function has either called it’s rejected or resolved function. If resolved is called, the async functions following line (const firstSearchResults = searchData.items[0]["login"])) will only then be executed or if the rejected function is called, an exception will be thrown.

Summing Up

With Async / Await now standard and stable in the latest versions of Node.JS and Chrome, there has not been a better time to use this awesome feature!

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store