Async / Await Warm Up

paul christophe
HackerNoon.com
Published in
2 min readNov 28, 2016

--

If you’re used to using Promises, async / await is an elegant way to make your javascript more legible. It evaluates as synchronous code but returns a promise that executes on the next microtask.

Let’s jump right in and poke around. Open up the babel repl and code along with me. Take a simple function and add async to the beginning:

You’ll see that calling this function returns a Promise rather than “hello”. This is because anything returned from an async function is automatically wrapped in a promise. In order to log “hello” we could do this:

OK so what about await?

First, write a function that returns a promise:

Pretty straight forward, we map over an array after given amount of time. Now let’s use that in an async function.

You’ll see that “hello” is logged before we are returned our array. Our async function is non-blocking.

It’s important to note that await must always be inside of an async closure. Calling it in the global scope or in a function without the the async keyword will throw an error.

Handling errors

Imagine something goes wrong with our original mapLater function:

Currently, we have no way of surfacing the error in our async addAndMultiply function. When we run it, the function will fail silently. To handle errors, one solution is to use try / catch:

Since errors bubble up by default, another practical solution would be to handle errors at the async entry point:

Real World

Use it today! So many great libraries provide APIs that return promises. For example, if you’re using fetch to retrieve data, you can start doing things like:

To use async / await now you’ll need to use Babel’s transform-async-to-generator.

--

--