What are the states of a Promise, and what happens if you resolve it twice?
A promise is pending, then becomes fulfilled with a value or rejected with a reason. Once settled it never changes, so later resolve or reject calls are ignored.
Explain it simply
A Promise is like an IRCTC booking request. At first it is pending. Then it ends one of two ways: the ticket is confirmed (fulfilled) or the booking fails (rejected). Fulfilled or rejected together are called settled.
The key rule is that a settled promise is locked. Its result can be read any number of times but can never change. If code calls resolve or reject again, JavaScript quietly ignores it.
The .then handlers always run asynchronously, even on an already-settled promise, as microtasks after the current code finishes.
Example
const p = new Promise((resolve, reject) => {
resolve("done");
reject(new Error("ignored"));
resolve("again");
});
p.then((v) => console.log(v)); // Output: done
p.then((v) => console.log(v + "!")); // Output: done!
console.log("sync first"); // Output: sync firstThe real printed order is sync first, then done, then done!, because the handlers run after the current code. A promise can be read by many handlers.
What to say in the interview
"A promise starts pending and settles exactly once, either fulfilled with a value or rejected with a reason. After that it is immutable, and further resolve or reject calls are ignored. Handlers run asynchronously as microtasks."
Common mistakes
- Thinking a rejected promise can be retried by calling resolve later.