What are Promises and how do they work in JavaScript?
Understand how Promises handle asynchronous operations in JavaScript and replace callback hell.
Advertisement
Question
What are Promises and how do they work in JavaScript?
Answer
A Promise represents a value that may be available now, later, or never.
It has three states: pending, fulfilled, and rejected.
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Done!'), 1000);
});
promise.then((result) => console.log(result)); // Done!
They make asynchronous code readable and eliminate callback hell.
Real-World Example
API calls, animations, file I/O, or timers all use Promises under the hood.
Quick Practice
Create a Promise that resolves after 2 seconds and logs “Task completed”.
Summary
Promises simplify async control flow — they’re the foundation of modern async JavaScript.
Do Promises execute immediately?
Yes, the executor function inside a Promise runs immediately when created.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement