Async or await are the most important features learning JavaScript. In this JavaScript crashcourse we have explained with code examples Arrow Functions, Destructuring, Rest Operators, Template Literal Classes & Inheritance, Modules and promises and now finallyAync/Await.
The important feature of Async/await is that it provides a more synchronous-looking way to work with asynchronous code. Here is a code example given below that shows how exactly it works. By going through this feature you can see you have been through most important aspects of JavaScript fundamentals:
async function fetchUserData(userId) { try {
const user = await fetchData(`https://api.example.com/users/${userId}`); const posts = await fetchData(`https://api.example.com/posts?
userId=${userId}`);
return { user, posts };
} catch (error) {
console.error('Error fetching user data:', error); throw error;
}
}
// Usage
fetchUserData(1)
.then(data => console.log(data))
.catch(error => console.error(error));
// Parallel execution
async function fetchMultipleUsers(userIds) { try {
const userPromises = userIds.map(id =>
fetchData(`https://api.example.com/users/${id}`)); const users = await Promise.all(userPromises); return users;
} catch (error) {
console.error('Error fetching multiple users:', error); throw error;
}
}
Best Practices
- Always use try/catch blocks with async/await to handle errors. Use Promise.all() for concurrent async operations.
- Consider using Promise.allSettled() when you want to wait for all promises to settle.
Common Pitfalls:
- Forgetting that async functions always return a promise. Using await outside of an async function.
- Not handling errors properly in async functions.











