Skip to main content

Command Palette

Search for a command to run...

Async/Await in JavaScript: Writing Cleaner Asynchronous Code

Updated
β€’2 min readβ€’View as Markdown

As JavaScript applications became more complex, handling asynchronous operations (like API calls, file reading, or timers) became harder to manage. This led to messy code structures like callback hell and complicated promise chains.

To solve this, async/await was introduced in ES2017 β€” making asynchronous code look and behave more like synchronous code.

πŸš€ Why Async/Await Was Introduced

Before async/await, developers used:

  1. Callbacks (😡 Callback Hell) getData(function(a) { processData(a, function(b) { saveData(b, function(c) { console.log(c); }); }); });

  2. Promises (Better, but still complex) getData() .then(processData) .then(saveData) .then(console.log) .catch(console.error);

Even with promises, code could become hard to read and debug.

πŸ‘‰ Async/await was introduced to make asynchronous code:

More readable Easier to write Easier to debug βš™οΈ How Async Functions Work

An async function always returns a Promise.

Example: async function greet() { return "Hello, World!"; }

This is equivalent to:

function greet() { return Promise.resolve("Hello, World!"); } Key Points: async makes a function return a promise You can use await only inside async functions ⏳ The Await Keyword Concept

The await keyword pauses execution until a promise is resolved.

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

πŸ‘‰ await makes asynchronous code behave like synchronous code.

❗ Error Handling with Async Code

Instead of .catch(), async/await uses try...catch.

Async/await is often called syntactic sugar over promises.

πŸ‘‰ Why? Because:

It doesn’t replace promises It just provides a cleaner way to write them

Under the hood:

await somePromise;

is still using .then() internally.

πŸ‘‰ Looks just like normal step-by-step logic!

🧠 Async Function Execution Flow (Concept) Function starts execution Hits await Pauses execution Waits for promise to resolve Resumes execution πŸ” Promise vs Async/Await Flow (Concept)

Promises Flow:

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

Async/Await Flow:

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

🧩 Simple Real-World Example

async function orderFood() {

console.log("Ordering food...");

const food = await new Promise(resolve => {

setTimeout(() => resolve("Pizza πŸ•"), 2000); });

console.log("Received:", food);

}

orderFood();

Conclusion

Async/await has transformed how developers handle asynchronous code in JavaScript.

Key Takeaways: Makes code cleaner and easier to read Eliminates complex promise chains Improves error handling with try/catch Built on top of promises (not a replacement)

πŸ‘‰ If you're writing modern JavaScript, async/await is essential.