TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses · 55.7K subscribers
Post #3187 4.34K
✅ Advanced JavaScript Part-2: Async JS (Callbacks, Promises, Async/Await) ⚙️⏳

Understanding how JavaScript handles asynchronous code is key to writing smooth, non-blocking apps.

1️⃣ Callbacks
A function passed as an argument and called after an operation finishes.

function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
}
fetchData(result => console.log(result)); // Data loaded

❗ Problems: Callback hell, hard to debug

2️⃣ Promises
Promises are cleaner and solve callback hell.

const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("Data fetched"), 1000);
});
};

fetchData()
.then(res => console.log(res))
.catch(err => console.error(err));

🔹 .then() handles success
🔹 .catch() handles error

3️⃣ Async/Await
Simplifies Promises using synchronous-looking code.

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

async function getData() {
const result = await fetchData();
console.log(result);
}
getData(); // Data received


🧠 Real Use Case: Fetching API data
async function loadUser() {
try {
const res = await fetch("https://api.example.com/user");
const data = await res.json();
console.log(data);
} catch (error) {
console.error("Error:", error);
}
}


🎯 Practice Tasks:
• Convert a callback to a promise
• Write a function that uses async/await
• Handle errors with .catch() and try-catch

💬 Tap ❤️ for more
  • ❤ 12
More from @javascript_courses
  1. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  2. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  3. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  4. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  5. Oct 4, 2026DSA Handwritten Notes
  6. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these st…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →