🎯 What is Async/Await?

Async/await is a cleaner, simpler way to handle Promises in JavaScript. It makes asynchronous code look and behave like synchronous code.


πŸ“Œ Key Concepts

1. Async Functions Always Return a Promise

`async function greet() { return "Aman"; }

console.log(greet()); // Promise { 'Aman' }`


2. Await Keyword - Wait for Promise to Resolve

async function getData() { const response = await fetch('<https://api.github.com/users>'); const data = await response.json(); console.log(data); }

What await does:


⚠️ Important Rules

Rule #1: Always Use Await Inside Async Functions

`// ❌ BAD - Don't use await at top level (freezes entire program) const response = await fetch(url);

// βœ… GOOD - Wrap in async function async function fetchData() { const response = await fetch(url); }`

Why? If you use await outside an async function, your entire program freezes waiting for the result.