Programming Languages JavaScript Subjective
Sep 18, 2025

Explain async/await in JavaScript and how it differs from Promises.

Detailed Explanation

async/await is syntactic sugar over Promises, making asynchronous code look more like synchronous code.

Async function:
async function fetchData() {
try {
const response = await fetch("/api/data");
const data = await response.json();
return data;
} catch (error) {
console.error("Error:", error);
}
}

Equivalent with Promises:
function fetchData() {
return fetch("/api/data")
.then(response => response.json())
.catch(error => console.error("Error:", error));
}

Benefits: Better readability, easier error handling with try/catch.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback