Question presented to candidate: "If a Promise's executor function calls resolve() and then, a moment later, calls reject() as well — what actually happens? Does the Promise genuinely change from fulfilled to rejected?"
What a strong answer should cover:
- 📌 Interview term: the three Promise states — pending (not yet settled), fulfilled (completed successfully, with a value), and rejected (failed, with a reason) — every Promise starts pending and can transition to exactly one of the other two states.
- 📌 Interview term: the real, direct answer to the prompt — verified directly: once a Promise is settled (fulfilled or rejected), its state is genuinely permanent — calling
resolve/rejectagain afterward is genuinely a no-op, silently ignored. A Promise resolved first with"first value"and then immediatelyresolved again with a different value genuinely keeps the FIRST value — the second call has zero effect. - 📌 Interview term:
.then()always returns a NEW Promise — verified directly: chaining.then()genuinely never returns the same Promise object — this is exactly the real mechanism that makes Promise chaining work at all. - 📌 Interview term: a thrown error inside
.then()produces a rejected Promise — verified directly: throwing inside a.then()callback genuinely does NOT crash the program — it genuinely produces a rejected Promise, catchable by a.catch()further down the chain. - A precise answer names that resolving a Promise WITH another Promise (or any "thenable") genuinely flattens/unwraps it rather than nesting — verified directly, resolving with an already-resolved inner Promise genuinely produces the inner Promise's own real value, not a Promise-wrapped-in-a-Promise.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering the prompt's own resolve-then-reject scenario (state permanence) is the strong signal.
Code / implementation expected: Yes — a real Promise where resolve is called twice (with a second, different value) is the clearest, most convincing demonstration of state permanence.