Skip to curriculum
Jump to the editor

13.7. A Promise: states and resolve once

Medium
Topics

This question is in

13. Promise from scratch
15–30 min

A promise is a small state machine. It starts pending, and it ends either fulfilled with a value or rejected with a reason. Once it has ended, it never changes again: that guarantee is what lets many parts of a program wait on the same promise and all see the same answer. Everything else about promises, then, chaining, await, is built on top of these few rules.

This is the first of six problems that build a promise class step by step. Here, write the core: the class MyPromise in my-promise.js, with its constructor and two read-only properties that let you see inside it.

Signature

JAVASCRIPT
class MyPromise {
  constructor(executor) // executor(resolve, reject) is called at once
  get state()           // "pending", "fulfilled" or "rejected"
  get value()           // the value or the reason; undefined while pending
}
  • executor: a function the constructor calls with two functions, resolve(value) and reject(reason).
  • state and value are getters: they report the promise's state, and cannot change it.

Requirements

  1. new MyPromise(executor) calls executor once, synchronously, before the constructor returns, with two functions: resolve and reject.
  2. A new promise has state "pending" and value undefined.
  3. resolve(value) makes it "fulfilled" with that value (the same object); reject(reason) makes it "rejected" with that reason. Any value counts, undefined and 0 included.
  4. Only the first call counts. After a promise has settled, every later call to resolve or reject is ignored.
  5. If executor throws, the promise is rejected with the thrown error. A throw after the promise has settled changes nothing.
  6. resolve and reject work when called later (after the constructor has returned), and as plain functions passed around, for example to setTimeout.
  7. Each promise has its own state: settling one never touches another.
  8. A non-function executor throws a TypeError. Calling MyPromise without new throws a TypeError too.
  9. Code outside cannot change state or value: assigning to them does nothing.
  10. The executor's return value is ignored.

Examples

JAVASCRIPT
const p = new MyPromise((resolve) => resolve(42));
console.log(p.state, p.value); // "fulfilled" 42
JAVASCRIPT
const q = new MyPromise((resolve, reject) => {
  reject(new Error("no"));
  resolve("ignored");
});
console.log(q.state, q.value.message); // "rejected" "no"
JAVASCRIPT
const r = new MyPromise((resolve) => setTimeout(resolve, 100, "later"));
console.log(r.state);  // "pending"
// 100 ms later: r.state is "fulfilled", r.value is "later"
JAVASCRIPT
const s = new MyPromise(() => { throw new Error("broken"); });
console.log(s.state, s.value.message); // "rejected" "broken"

Environment

  • Plain JavaScript in a browser page; the tests use the global class MyPromise.
  • Do not use the built-in Promise inside MyPromise.
  • then, catch, finally and following a thenable passed to resolve are out of scope here: in this stage resolve stores whatever it is given. They are problems 13.8 to 13.11.

Hints

  1. resolve is called as a plain function, with no object before the dot. How can it still reach the right promise?
  2. What single check, at the start of settling, makes every call after the first one do nothing?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…