Async Await
This lesson explains Async Await in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.
JavaScript Async Await Overview
JavaScript async and await provide a cleaner way
to write asynchronous code using promises. They make asynchronous JavaScript
look more like synchronous code, which improves readability and makes API
calls, timers, and background tasks easier to understand.
The async keyword declares a function that always returns a
promise. The await keyword pauses execution inside an async
function until a promise is resolved or rejected. Async await is widely used
in frontend applications, Node.js APIs, React, Angular, Vue, Next.js, and
modern full-stack JavaScript development.
| Concept | Description | Common Usage |
| async | Creates a function that returns a promise. | API functions and async workflows. |
| await | Waits for a promise result inside an async function. | Fetching data and sequential tasks. |
| try...catch | Handles async errors. | API error handling. |
| Promise | Represents a future success or failure value. | Foundation of async await. |
| Parallel Async | Runs multiple async tasks together. | Promise.all with await. |
JavaScript Async Await Example
async function getUser() {
try {
const response = await fetch("/api/users/1");
const user = await response.json();
console.log(user);
} catch (error) {
console.log("Failed to load user");
}
}
getUser();
This example uses async and await to fetch user
data from an API. The try...catch block handles errors if the
request fails.
Top 10 JavaScript Async Await Examples
The following examples show common async await patterns used in modern
JavaScript applications, including API calls, error handling, parallel
requests, form submissions, and dynamic imports.
| # | Example | Syntax |
| 1 | Async Function | async function getData() { return "Data"; } |
| 2 | Await Promise | const data = await getData(); |
| 3 | Fetch API | const response = await fetch("/api/users"); |
| 4 | Read JSON | const users = await response.json(); |
| 5 | Error Handling | try { await getData(); } catch (error) { console.log(error); } |
| 6 | Async Arrow Function | const loadData = async () => { return await getData(); }; |
| 7 | Parallel Requests | const result = await Promise.all([getUsers(), getOrders()]); |
| 8 | Sequential Requests | const user = await getUser(); const orders = await getOrders(user.id); |
| 9 | Dynamic Import | const module = await import("./utils.js"); |
| 10 | Finally Cleanup | try { await saveData(); } finally { loading.hide(); } |
Popular Real-World Async Await Examples
These examples show how async await is used in real projects for API
requests, form handling, authentication, dashboards, lazy loading, and error
handling.
| Scenario | Async Await Pattern |
| Load Users | const users = await fetchUsers(); |
| Submit Contact Form | const response = await fetch("/api/contact", options); |
| Login User | const token = await login(email, password); |
| Load Dashboard Data | const data = await Promise.all([getStats(), getUsers()]); |
| Handle API Error | try { await fetchUsers(); } catch (error) { showError(); } |
| Show Loading State | try { loading.show(); await getData(); } finally { loading.hide(); } |
| Read JSON Response | const data = await response.json(); |
| Upload File | const result = await uploadFile(formData); |
| Lazy Load Feature | const chart = await import("./Chart.js"); |
| Save Settings | await saveSettings(settings); |
Best Practices
- Use
async and await for readable asynchronous code. - Always handle async errors with
try...catch. - Use
Promise.all() for independent tasks that can run in parallel. - Use sequential
await only when one result depends on another. - Show loading and error states in user interfaces.
- Validate API responses before using returned data.
- Keep async functions small and focused.
- Avoid mixing
then() chains and await unnecessarily.
Common Mistakes
- Using
await outside an async function in unsupported contexts. - Forgetting to add
await before a promise. - Not handling rejected promises with
try...catch. - Running independent async tasks sequentially instead of using
Promise.all(). - Forgetting to return data from an async function.
- Ignoring loading and error states.
- Using async await when simple synchronous code is enough.
Key Takeaways
async functions always return promises. await pauses execution inside async functions until a promise settles. - Async await makes promise-based code easier to read.
try...catch is used to handle async errors. Promise.all() improves performance for independent async tasks. - Async await is essential for API calls, form submissions, and modern web apps.
Pro Tip
Use Promise.all() with await when multiple API
calls are independent. This runs them at the same time and improves
performance compared to waiting for each request one by one.