Programming Languages JavaScript Subjective
Sep 25, 2025

What are Promises and async/await?

Detailed Explanation

Promises represent the eventual completion or failure of an asynchronous operation.

Promise States:
- Pending - initial state
- Fulfilled - operation completed successfully
- Rejected - operation failed

Creating Promises:


const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Success!");
}, 1000);
});

Using Promises:


promise
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log("Done"));

Async/Await (ES2017):


async function fetchData() {
try {
const result = await promise;
console.log(result);
} catch (error) {
console.error(error);
}
}

Benefits:
- Cleaner than callbacks
- Better error handling
- Avoids callback hell
- More readable asynchronous code

Discussion (0)

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

Share Your Thoughts
Feedback