async/await
📖 Concept
async/await is syntactic sugar over Promises that makes asynchronous code look and behave like synchronous code. It's the modern, preferred way to handle async operations.
async function — Always returns a Promise. If you return a value, it's wrapped in Promise.resolve().
await — Pauses execution of the async function until the Promise resolves. Can only be used inside async functions (or top-level in modules).
🏠 Real-world analogy: async/await is like reading a recipe step by step. "Mix the flour, AWAIT until it's smooth, then add eggs, AWAIT until blended." Each step finishes before the next starts, but the kitchen (event loop) isn't blocked — the chef can check other things while waiting.
💻 Code Example
1// Basic async/await2async function fetchUser(id) {3 const response = await fetch(`/api/users/${id}`);4 const user = await response.json();5 return user; // Automatically wrapped in Promise.resolve()6}78// Error handling with try/catch9async function loadData() {10 try {11 const user = await fetchUser(1);12 const posts = await fetch(`/api/posts?userId=${user.id}`);13 const data = await posts.json();14 return { user, posts: data };15 } catch (error) {16 console.error("Failed to load:", error.message);17 throw error; // Re-throw for caller to handle18 } finally {19 console.log("Loading complete");20 }21}2223// Parallel async operations (DON'T await sequentially!)24// ❌ BAD — sequential (slow!)25async function fetchSequential() {26 const users = await fetch("/api/users"); // Wait 1s27 const posts = await fetch("/api/posts"); // Wait another 1s28 // Total: 2 seconds29}3031// ✅ GOOD — parallel (fast!)32async function fetchParallel() {33 const [users, posts] = await Promise.all([34 fetch("/api/users"),35 fetch("/api/posts")36 ]);37 // Total: ~1 second (parallel)38}3940// Async iteration41async function* generatePages(url) {42 let page = 1;43 while (true) {44 const res = await fetch(`${url}?page=${page}`);45 const data = await res.json();46 if (data.length === 0) break;47 yield data;48 page++;49 }50}5152// for await...of53async function getAllPages() {54 for await (const page of generatePages("/api/items")) {55 console.log("Page:", page);56 }57}5859// Top-level await (ES Modules only)60// const config = await fetch("/config.json").then(r => r.json());6162// Async class methods63class ApiClient {64 async get(url) {65 const res = await fetch(url);66 if (!res.ok) throw new Error(`HTTP ${res.status}`);67 return res.json();68 }69}
🏋️ Practice Exercise
Mini Exercise:
- Convert a Promise chain to async/await
- Implement parallel data fetching with proper error handling
- Write an async function that retries a fetch up to 3 times
- Build a simple async queue that processes tasks one at a time
⚠️ Common Mistakes
Awaiting in a loop (sequential) when operations are independent — use
Promise.allfor parallel executionForgetting try/catch — unhandled rejections in async functions go to
.catch()or crash the processUsing
awaitat the top level without modules — only works in ES Modules or Chrome DevToolsReturning
await promisevsreturn promise— they're almost the same, butreturn awaitin try/catch catches the errorForgetting that
asyncfunctions always return a Promise — even if you return a plain value
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for async/await. Login to unlock this feature.