⏳ 8. Async / Await in JavaScript

🔹 1. Async Function

`async` keyword function ke aage lagta hai, jisse wo hamesha promise return karta hai.

async function greet() {
  return "Hello Zohaib";
}

greet().then(msg => console.log(msg)); // Hello Zohaib

🔹 2. Await Keyword

`await` sirf async function ke andar use hota hai. Ye kisi promise ko wait karta hai bina callback ke.

function delay() {
  return new Promise(resolve => {
    setTimeout(() => resolve("Waited 2 sec"), 2000);
  });
}

async function run() {
  const result = await delay();
  console.log(result); // Waited 2 sec
}

run();

🔹 Bonus: Error Handling

`try/catch` use karke async/await ke errors handle kar sakte ho cleanly.

async function fetchData() {
  try {
    const res = await fetch("https://api.example.com");
    const data = await res.json();
    console.log(data);
  } catch (err) {
    console.error("Error:", err);
  }
}