Skip to main content

Command Palette

Search for a command to run...

Async/Await in JavaScript Explained with Examples (Beginner to Advanced Guide)

Updated
β€’4 min read
Async/Await in JavaScript Explained with Examples (Beginner to Advanced Guide)

πŸ“Œ Introduction

JavaScript is single-threaded, but it handles asynchronous operations like API calls, file reading, and timers using mechanisms like callbacks and promises. However, managing async code with callbacks and .then() chains can quickly become messy and hard to read.

This is where async/await comes in β€” a modern and cleaner way to write asynchronous code.


❓ Why Async/Await Was Introduced

Before async/await, developers mainly used:

1. Callbacks (❌ Problem: Callback Hell)

getData(function(a) {
  getMoreData(a, function(b) {
    getEvenMoreData(b, function(c) {
      console.log(c);
    });
  });
});

πŸ‘‰ Deep nesting β†’ Hard to read and maintain


2. Promises (Better, but still verbose)

getData()
  .then(a => getMoreData(a))
  .then(b => getEvenMoreData(b))
  .then(c => console.log(c))
  .catch(err => console.error(err));

πŸ‘‰ Cleaner than callbacks, but still chaining-heavy


βœ… Solution: Async/Await

Async/await was introduced to make asynchronous code look like synchronous code, improving readability and maintainability.


⚑ Async/Await = Syntactic Sugar Over Promises

πŸ‘‰ Important concept:

Async/await does NOT replace promises β€” it is built on top of them.


πŸ”§ How Async Functions Work

An async function always returns a promise.

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

greet().then(console.log);

πŸ‘‰ Output:

Hello Virat

Even though we returned a string, it is automatically wrapped inside a promise.


⏳ Await Keyword Concept

The await keyword pauses execution of an async function until a promise resolves.

Example:

function fetchData() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve("Data received");
    }, 2000);
  });
}

async function getData() {
  console.log("Fetching...");
  const result = await fetchData();
  console.log(result);
}

getData();

πŸ‘‰ Output:

Fetching...
(wait 2 seconds)
Data received

🧠 Key Points About await

  • Can only be used inside async functions

  • Waits for a promise to resolve or reject

  • Makes code look synchronous


πŸ”„ Execution Flow (Concept)

Promise Flow:

Start β†’ then() β†’ then() β†’ catch()

Async/Await Flow:

Start β†’ await β†’ await β†’ try/catch

πŸ‘‰ Much easier to understand and debug


⚠️ Error Handling with Async Code

Instead of .catch(), we use try...catch.

Example:

function fetchDataWithError() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("Error occurred!");
    }, 2000);
  });
}

async function getData() {
  try {
    const result = await fetchDataWithError();
    console.log(result);
  } catch (error) {
    console.error("Caught Error:", error);
  }
}

getData();

πŸ‘‰ Output:

Caught Error: Error occurred!

πŸ” Comparison: Promises vs Async/Await

Feature Promises (.then) Async/Await
Syntax Chain-based Looks synchronous
Readability Medium High
Error Handling .catch() try...catch
Debugging Harder Easier
Learning Curve Moderate Easier

πŸ’‘ Real-World Example (API Call)

Using Promises:

fetch("https://jsonplaceholder.typicode.com/posts/1")
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error(err));

Using Async/Await:

async function getPost() {
  try {
    const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

getPost();

πŸ‘‰ Cleaner, readable, and easy to maintain


πŸš€ Advantages of Async/Await

βœ” Cleaner code βœ” Better readability βœ” Easier debugging βœ” Avoids callback hell βœ” Structured error handling


⚠️ Common Mistakes

❌ Forgetting await

const data = fetch(url); // returns promise, not actual data

❌ Using await outside async

await fetch(url); // ERROR

🎯 When to Use Async/Await

  • API calls

  • Database operations

  • File handling

  • Any asynchronous workflow


🧩 Final Thoughts

Async/await transforms asynchronous programming in JavaScript by making it:

πŸ‘‰ Simple πŸ‘‰ Clean πŸ‘‰ Easy to understand

If you already know promises, learning async/await is the next step to writing professional JavaScript code.


πŸ“Œ Conclusion

Async/await is not just a feature β€” it's a best practice for handling asynchronous operations in modern JavaScript.

More from this blog

Dev Blog by Vishal

36 posts