Async and Await
Introduction
Asynchronous code can become difficult to follow when it has a lot of things going on. async
and await
are two keywords that can help make asynchronous read more like synchronous code. This can help code look cleaner while keeping the benefits of asynchronous code.
For example, the two code blocks below do the exact same thing, they both get information from a server, process it, and return a promise.
The second example looks much more like the kind of functions you are used to writing, however, did you notice the async
keyword before the function declaration? How about the await
keyword before server.getPeople()
?
Learning outcomes
How do you declare an
async
function?What does the
async
keyword do?What does the
await
keyword do?What is returned from an
async
function?What happens when an error is thrown inside an
async
function?How can you handle errors inside an
async
function?
Async
The async
keyword is what lets the javascript engine know that you are declaring an asynchronous function, this is required to use await
inside any function. When a function is declared with async
, it automatically returns a promise, returning in an async
function is the same as resolving a promise, likewise, throwing an error will reject the promise.
An important thing to understand is async
functions are just syntactical sugar for promises
.
The async
keyword can also be used with any of the ways a function can be created, said differently: it is valid to use an async
function anywhere you can use a normal function. Below you will see some examples that may not be intuitive, if you don't understand them, come back and take a look when you are done with the assignments.
Await
await
is pretty simple: it tells javascript to wait for an asynchronous action to finish before continuing the function. It's like a 'pause until done' keyword. The await
keyword is used to get a value from a function where you would normally use .then()
. Instead of calling .then()
after the asynchronous function, you would simply assign a variable to the result using await
, then you can use the result in your code as you would in your synchronous code.
Error handling
Handling errors in async
functions is very easy. Promises have the .catch()
method for handling rejected promises, and since async functions just return a promise, you can simply call the function, and append a .catch()
method to the end.
But there is another way: the mighty try/catch
block! If you want to handle the error directly inside the async
function, you can use try/catch
just like you would inside synchronous code.
Doing this can look messy, but it is a very easy way to handle errors without appending .catch()
after your function calls. How you handle the errors is up to you, and which method you use should be determined by how your code was written. You will get a feel for what needs to be done over time. The assignments will also help you understand how to handle your errors.
Practice
Remember the Giphy API practice project? (If not, you should go back and complete the API lesson) We are going to convert the promise based code into async/await
compatible code. Here's a refresher of the code we are starting with:
Since await
does not work on the global scope, we will have to create an async
function that wraps our API call to Giphy.
Now that we have a function that is asynchronous, we can then start refactoring from using promises to using await
:
Since response
is still the same object we have passed to the .then()
block at the start, we still need to use the .json()
method, which in turn returns a promise. Because .json()
returns a promise, we can use await
to assign the response to a variable.
To use this function, we just simply need to call it with getCats()
in our code.
This code will behave exactly like the code from the last lesson, it just looks a bit different after refactoring. async/await
are very useful tools when it comes to cleaning up asynchronous javascript code. It is important to remember async/await
are just promises written in a different way. Do the assignments below, and dive deeper into the understanding of async/await
.
Assignment
Read this article for a solid introduction to async/await. this article also has some good examples of its use.
Read this article for a more in-depth look at async/await, including how to handle errors.
Watch this video for a good overview on async/await and it's purpose, along with a special trick.
Additional resources
This video is an example of how you can change callbacks, to promises, to async/await.
This video gives a comprehensive view of Promises, async, and await.
Last updated