JavaScript: async and await

An async function returns a Promise, and await pauses that function until another Promise settles.

What you will learn

Minimal example

async function loadItems() {
  try {
    const response = await fetch('/api/items');
    if (!response.ok) throw new Error('Request failed');
    return await response.json();
  } catch (error) {
    console.error(error);
    return [];
  }
}

The function still returns a Promise. Callers can await it or attach then and catch.

Common mistakes