Back

Async/Await

JavaScript is single-threaded: it does one thing at a time. But some things take time, like fetching data from an API. Async/await lets you wait for those things without freezing the whole program. Think of it like ordering food at a restaurant. You place the order (start the request), do other things while waiting, and when it arrives (data comes back) you eat (use the data). Key concepts: - `async` makes a function asynchronous - `await` pauses until the promise resolves - Always wrap in try/catch to handle errors

async function getUser() {
  try {
    const response = await fetch('https://api.example.com/user')
    const data = await response.json()
    console.log(data)
  } catch (error) {
    console.log("Something went wrong:", error)
  }
}

Roadmap