Skip to the solution
MediumFrontend6 min read

What is a Promise and what are its states?

922 views

The problem

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/reject again afterward is genuinely a no-op, silently ignored. A Promise resolved first with "first value" and then immediately resolved 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.

  • promises

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a Promise's settlement is genuinely permanent β€” a second resolve() call has zero effect, and .then() always returns a new Promise

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 60 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Explain event delegation.

Continue