Episode 4: Async / Await
πΉ Introductionβ
async β A keyword in JavaScript used to create asynchronous functions.
Async functions always return a Promise, regardless of what you return inside them.
π§© Example 1: Async Function Always Returns a Promiseβ
async function getData() {
return "Namaste JS";
}
const dataPromise = getData();
console.log(dataPromise);
dataPromise.then((res) => console.log(res));
π Output:
Promise {<fulfilled>: "Namaste JS"}
Namaste JS
β Even though we're returning a string, JavaScript automatically wraps it in a Promise.
πΉ Handling Promises Using Async / Awaitβ
const p = new Promise((resolve, reject) => {
resolve("Promise Resolved Value!");
});
async function handlePromise() {
const val = await p;
console.log(val);
}
handlePromise();
π‘ await pauses the execution of the async function until the promise resolves.
π Deep Dive: Promise Resolution vs Async / Awaitβ
π§© Normal .then() Approachβ
const p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved value!");
}, 10000);
});
function getData() {
p.then((res) => console.log(res));
console.log("Namaste JS");
}
getData();
π Output:
Namaste JS
// after 10 seconds
Promise resolved value!
async function getData() {
return "Namaste JS";
}
const dataPromise = getData();
console.log(dataPromise);
dataPromise.then((res) => console.log(res));
π Output:
Promise {<fulfilled>: "Namaste JS"}
Namaste JS
β Even though weβre returning a string, JavaScript automatically wraps it in a Promise.
πΉ Handling Promises Using Async / Awaitβ
const p = new Promise((resolve, reject) => {
resolve("Promise Resolved Value!");
});
async function handlePromise() {
console.log("Hello World");
const val = await p;
console.log("Namaste JavaScript");
console.log(val);
const val2 = await p;
console.log("Namaste JavaScript 2");
console.log(val2);
}
handlePromise();
π Output:
Hello World
// after 10 seconds
Namaste JavaScript
Promise resolved value!
Namaste JavaScript 2
Promise resolved value!
β‘ Key Insight:
βTime, tide, and JavaScript wait for none.β
JavaScript doesnβt literally pause execution. When it sees an await, it suspends the function execution and lets the event loop handle other tasks until the awaited Promise resolves.
πΉ Example with Multiple Promisesβ
const p1 = new Promise((resolve) => {
setTimeout(() => resolve("Promise 1 Resolved!"), 1000);
});
const p2 = new Promise((resolve) => {
setTimeout(() => resolve("Promise 2 Resolved!"), 5000);
});
async function handlePromise() {
console.log("Hello World");
const v1 = await p1;
console.log("Namaste JavaScript");
console.log(v1);
const v2 = await p2;
console.log("Namaste JS 2");
console.log(v2);
}
handlePromise();
π Output:
Hello World
// after 1 second
Namaste JavaScript
Promise 1 Resolved!
// after 5 seconds
Namaste JS 2
Promise 2 Resolved!
π Real-World Example: Fetching API Dataβ
const API_URL = "https://api.github.com/users/prakhaaar";
async function handlePromise() {
try {
const data = await fetch(API_URL);
const jsonValue = await data.json();
console.log(jsonValue);
} catch (err) {
console.log("Error:", err);
}
}
handlePromise().catch((err) => console.log(err));
π Notes:
fetch()returns a Promise.response.json()also returns a Promise.- Hence we use two awaits.
- We can handle errors via
try...catchor.catch().
π¬ Interview Insights: Async / Awaitβ
1οΈβ£ async keyword is used to create asynchronous functions.
2οΈβ£ await can only be used inside an async function.
3οΈβ£ Itβs used to wait for promises to resolve in a cleaner, synchronous-like manner.
4οΈβ£ Helps avoid callback hell or long promise chains.
5οΈβ£ Under the hood, itβs just syntactic sugar over .then() and .catch().
βοΈ Async / Await vs .then() / .catch()β
| Feature | Async / Await | Promise .then() |
|---|---|---|
| Syntax | Synchronous-looking | Chained callbacks |
| Readability | Very clean and readable | Becomes messy with many chains |
| Error Handling | try...catch | .catch() |
| Under the Hood | Built on Promises | Base Promise handling |
π‘ Async/Await is preferred for modern codebases β cleaner flow, easier debugging, and fewer nested callbacks.
π§Ύ Recapβ
β
async β always returns a Promise
β
await β pauses async function until Promise resolves
β
JS doesnβt block, it suspends function execution
β
Handle errors with try...catch
β
Perfect for sequential async operations
Watch Live On YouTube below:
