JavaScript Async/Await: Tutorial & Quiz

What is `async`/`await`?

`async` and `await` are used in JavaScript to handle asynchronous operations. These keywords allow you to write asynchronous code in a way that looks and behaves like synchronous code.

Understanding `async`

An `async` function is a function that always returns a `Promise`. It allows you to use `await` inside the function, which pauses the function execution until the promise is resolved.

async function fetchData() { /* fetch data */ }

Understanding `await`

The `await` keyword can only be used inside an `async` function. It pauses the execution of the function until the promise is resolved or rejected.

let result = await fetch('https://api.example.com/data');

Example: Fetching Data from an API

In the example below, we will fetch data from a simulated API using `async`/`await` and display the result.

Quiz: Test Your Knowledge

1. What does the `async` keyword do?





2. Where can you use `await`?




3. What does `await` do in an `async` function?