We'll cut the name here, and we'll introduce a variable, and then assign the function expression. That's it. Now we have an async function expression which does the same as our previous function declaration.
We could also write this function as an arrow function using the arrow function syntax and the async keyword, like this. As you can see, the async keyword works with all function types, so it's a matter of personal preference or style, whichever one you pick. I prefer to use function declarations wherever possible so I can make use of function hoisting.
Let's now look at a good use case for an async function expression. The await keyword can only be used within asynchronous functions, so we cannot use it at the top level of a file. Therefore, this syntax is not valid. What we need here is an async function wrapper around these lines.
Finally, let's implement an async class method. First of all we're going to need a class, so let's define a GitHub API client class and that class has a method called fetch user. Within that method we have the three lines from before.
We can now put the async keyword in front of the method name to make the method asynchronous. The same syntax is used within object literals to create asynchronous methods using the ES2015 shorthand method syntax. Down below, we're going to create an instance of the class and call it client. We'll say client.fetchUser.
Let's now run the program to make sure everything is working as expected after we've made all these changes. Sure enough, everything is working just fine.