Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Convert Any Function into an Asynchronous Function

    Marius SchulzMarius Schulz

    Any function can be made asynchronous, including function expressions, arrow functions, and methods. This lesson shows the syntax for each of the function types.

    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 JavaScript allows us to convert any function into an async function. So far we've worked with function declarations but we haven't yet looked at function expressions, error functions, or methods. Let's go ahead and convert this function declaration into a function expression.

    00:18 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.

    00:32 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.

    00:54 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.

    01:13 We'll say async function and then indent these lines and we're immediately going to be invoking this function. What we have here is the classic JavaScript IIFE pattern -- the immediately-invoked function expression. If you wanted to you could even make this an arrow function.

    01:33 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.

    01:54 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.

    02:18 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.

    Discuss

    Discuss