Join egghead, unlock knowledge.

Want more egghead?

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

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Write Async Functions with the async/await Operators


    async / await should really be thought as a better way to use Promises reliably and safely with less chances of programming errors.

    This lesson covers the fundamentals of annotating a function as async. An async function gets the chance to use the await keyword. This lesson covers the usages of these keywords.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: The make a function async, all you need to do is prefix it with the async keyword. Async can be used with simple functions, error functions, and class methods. Let's discuss the nature of async functions in isolation by focusing in on a simple function.

    An async function always returns a promise. The promise resolves to the value that is returned by the function. Here, the function returns an implicit return of all JavaScript functions, which is undefined. If we log out the value, you can see that is going to be undefined.

    If we return a value from that function, of course, the promise will resolve to that value. TypeScript is smart enough to infer the return type for async functions to be the promise of the returned values. With the return value of async functions out of the way, let's focus in on the body of an async function.

    We set up Node.js to ignore any unhandled promise rejections to have a clean console. Next, we set up a few promises to use in our async function. We create a variable that is not a promise. We create a promise that will resolve. Finally, we also create a promise that will reject.

    Async functions get access to the await operator in their function bodies. The await operator can be applied to any variable. If the variable is not a promise, the value returned for the await operator is same as the variable.

    If the variable is a promise, execution in the function body pauses until the fate of the promise is determined. If the promise resolves the value of the await operator, it's the resolve value of the promise. If the variable is a promise that gets rejected, the await operator throws an error in the body of the async function.

    This means no subsequent statements execute unless there is a catch construct, similar to try/catch in synchronous code. Let's go ahead and call this function to verify that it works as explained. The not a promise variable just resolves to itself. The promise that will resolve returns its final resolved value, and the promise that gets rejected interacts with the try/catch as expected.

    If a promise takes some time to settle in its fate, execution at the await operator pauses until the fate is determined. Here, we are going to wait five seconds before determining the fate of this local promise. If we go ahead and run this code, you can see that the execution pauses for five seconds, and then the next line, done waiting, is executed.

    Essentially, async await allows you write asynchronous code based on promises in a manner that allows you to reuse your synchronous code writing skills.