Async/Await

Learn the modern way to write asynchronous JavaScript. Async/await is quickly becoming the industry standard and it's an important tool to learn.

async/await

We’ve covered callbacks and promises. Now we get to the latest async feature that will change how we write asynchronous functions in JS.

This is a feature that truly makes asynchronous code much easier to write. Let’s dive right in.

Introduction

An asynchronous function is created using by writing the async keyword before a function. Here’s what it looks like for a standard function.

async function fn() {...}
const fn = async function() {...}

Here’s what it looks like for an arrow function.

const fn = async () => {...}

Inside an asynchronous function, we can use the await keyword to make the function pause and wait for some operation to complete.

Here, I’ve written a function wait that takes in a number and returns a string after that number of seconds. The function fn calls wait. When wait completes and resolves, fn continues executing.

Example

Get hands-on with 1400+ tech skills courses.