Explain the concept of promises in JavaScript.
💡 Model Answer
A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. It can be in one of three states: pending, fulfilled, or rejected. When a promise is fulfilled, it resolves with a value; when it is rejected, it resolves with a reason (an error). Promises allow you to attach callbacks using .then() for success and .catch() for failure, enabling a cleaner, more readable flow than nested callbacks.
Example:
const fetchData = () => new Promise((resolve, reject) => {
setTimeout(() => {
const data = { id: 1, name: 'Alice' };
resolve(data);
}, 1000);
});
fetchData()
.then(result => console.log(result))
.catch(err => console.error(err));Promises are especially useful for chaining asynchronous operations, handling errors in a single place, and integrating with async/await syntax, which internally uses promises to pause execution until the promise resolves.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500