Notes - Async Await
wait for it...July 17, 2017
If you are interested in checking out the spec then you can find the official documentation here. Also if you would like to follow along and contribute to new language features then you should take a look the ECMA TC39 group and their proposals repo to track all of this good stuff.
Alright, so this post is really just a culmination of notes and resources that I have taken the time to research around async await. This probably does not capture all of the features and possibilities of async await but hopefully, others find it useful.
Now, using Async Functions we can convert the above situation into something like this.
But what about error handling? Now we can use try/catch to wrap these async calls.
Now we will get the following error message
"Uh oh: result text should equal string".
This type we have already gone over but we should start with this one and move to the others.
In order to use an Async Function, one must use the keywords
await. Also important to note that Async Functions always return a Promise.
Below are two different ways of using async functions within expressions. One is assigned to a variable and the other is an Immediately Invoked Function Expression (IIFE).
It’s important to note that with the Async Function Expression we can also simplify them a bit by using Arrow functions. Below is a quick example.
You also have the ability to use async functions within classes as well. Here is a quick example.
Async Functions are new and exciting but they also present some new scenarios that we need to understand and account for. Rather than try and explain all of these myself I’d rather point you in the direction of Axel Rauschmayer who wrote up a nice async function tips article highlighting all of these new scenarios and other gotchas.
Below is current browser support for Async Functions.
If you liked this article then please show some ❤️ with a follow below.