Ram

Lesson 6 of 6

Async JavaScript & Fetch

Request external data using promises and async/await.

Network requests take time, so JavaScript uses asynchronous patterns to avoid blocking the interface.

The fetch API returns a promise and is commonly combined with async/await for readable control flow.

Example

async function loadData() {
  const response = await fetch("/api/example");
  const data = await response.json();
  console.log(data);
}

Practice challenge

Describe how you would handle a failed fetch request with try/catch.

Previous
Take quiz