# async / await

> Write asynchronous code that looks and reads like synchronous code. Discover the modern standard for handling background tasks and the massive performance trap of sequential awaits.

Subject: JavaScript | Lesson 33 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/async-await
Previous: [Promises: then, catch & chaining](https://codewithanimation.in/javascript/promises.md) | Next: [Promise Combinators](https://codewithanimation.in/javascript/promise-combinators.md)

## Synchronous Style, Asynchronous Power

Introduced in ES8 (2017), `async` and `await` are syntactic sugar built directly on top of Promises.

They allow you to pause a function until a background task finishes, without needing to write any `.then()` callback chains.

## The try / catch Revival

Because `async / await` makes asynchronous code look synchronous, it brings back a massive feature that Callback Hell destroyed: the standard `try / catch` block.

You can now wrap a network request in a normal `try` block and immediately handle any rejection in the `catch` block, keeping your error handling centralized and readable.

## The Two Rules

1. You can only use the `await` keyword inside a function that is explicitly marked with the `async` keyword.

2. An `async` function *always* returns a Promise, even if you just `return 5`. JavaScript automatically wraps the return value in a resolved Promise.

## Common mistake: The Sequential Trap

The biggest mistake developers make with `await` is accidentally creating water-falls. If you need to fetch a user profile (takes 2 seconds) and fetch the latest news (takes 2 seconds), and you `await` them one after the other on separate lines, the total time will be 4 seconds.

If the news doesn't depend on the user data, they should be fired off at the exact same time (parallel) using `Promise.all()`, cutting the time in half!

## Tip: Top-Level Await

Historically, `await` was strictly banned outside of `async` functions. However, modern ES Modules now support 'Top-Level Await', allowing you to use `await` at the very root of a module file to pause the execution of the entire module until a dependency loads.

## Try it: Sequential vs Parallel Cost

Run two simulated network requests. Notice how sequential `await` calls stack their loading times, while parallel execution overlaps them.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/async-await

## Check yourself

### Question 1

What does an `async` function always return?

- A. A Promise
- B. The exact value you write after the return keyword
- C. undefined

Correct answer: A

No matter what you return inside an `async` function, JavaScript will automatically wrap it in a Promise.

- Why not B: Incorrect. The returned value is automatically wrapped in a Promise.
- Why not C: Incorrect. Even if you return nothing, it returns a Promise that resolves to undefined.

### Question 2

Why might writing multiple `await` statements on consecutive lines be bad for performance?

- A. Because each line waits for the previous one to finish, creating a sequential waterfall.
- B. Because `await` blocks the main thread, freezing the browser.
- C. Because it consumes too much memory.

Correct answer: A

If tasks don't depend on each other, waiting for them sequentially wastes time. They should be executed in parallel.

- Why not B: Incorrect. `await` pauses the function, but it does NOT block the Event Loop.
- Why not C: Incorrect. It is a time/speed issue, not a memory issue.

### Question 3

How do you handle errors when using `async / await`?

- A. Wrap the await calls in a standard try / catch block.
- B. Attach a .catch() method to the async function.
- C. Pass an error callback as the second argument.

Correct answer: A

One of the biggest benefits of `async / await` is the ability to use standard, synchronous-looking `try / catch` blocks for error handling.

- Why not B: Incorrect. While technically possible when calling it, the idiomatic way *inside* the function is try/catch.
- Why not C: Incorrect. That is the old Node.js callback pattern.

## Remember this

- `async / await` is syntax sugar on top of Promises.
- You can only use `await` inside an `async` function (or at the top level of a module).
- An `async` function always returns a Promise.
- `await` pauses the execution of the function until the Promise settles.
- Error handling is done using standard `try / catch` blocks.
- Avoid the sequential trap: don't `await` independent tasks one after the other.
