Skip to curriculum
Jump to the editor

13.1. Promise.resolve and Promise.reject

Easy
Topics

This question is in

13. Promise from scratch
10–20 min

Code that accepts "a value or a promise" needs one way to treat both the same. Promise.resolve(x) is that way: it gives you a promise whatever x is, so the next line can always call .then. Its partner Promise.reject(reason) gives you a promise that has already failed, which is handy in tests, in caches that store a failure, and in functions that must return a promise even when the input is wrong.

Write both of them yourself, as two plain functions in resolve-reject.js: promiseResolve(value) and promiseReject(reason). They must behave like the built-ins for every case below.

Signature

JAVASCRIPT
function promiseResolve(value) // returns a Promise
function promiseReject(reason) // returns a Promise
  • value: anything: a plain value, a promise, or a thenable (any object or function with a then method).
  • reason: anything, kept exactly as it is.
  • Both return a real Promise (result instanceof Promise is true).

Requirements

  1. promiseResolve always returns a Promise. A plain value (a number, undefined, null, an object) becomes the fulfilled value, the very same object, not a copy.
  2. A promise made by Promise itself (instanceof Promise, and its constructor is Promise) is returned unchanged: the same object, pending or settled. A thenable that only has a constructor property set to Promise is not a promise: it is adopted (requirement 4).
  3. Any other promise, such as one from a subclass of Promise, or one whose constructor property was changed, gives a new plain Promise that follows it.
  4. A thenable is adopted: the returned promise fulfills or rejects as the thenable's then says.
  5. The thenable's then is called later, in a microtask, with the thenable as this; never during the call to promiseResolve.
  6. If reading then throws, or then throws before it settles anything, the promise rejects with that error. A throw after it has settled changes nothing.
  7. An object whose then is not a function is just a value.
  8. promiseReject returns a new promise rejected with reason exactly: a promise or a thenable given as the reason is never unwrapped, and never returned itself.
  9. Neither function calls Promise.resolve or Promise.reject.

Examples

JAVASCRIPT
const p = Promise.resolve(1);
promiseResolve(p) === p;                  // true: the same promise
promiseResolve(42).then(console.log);     // 42
JAVASCRIPT
const thenable = { then(onFulfilled) { onFulfilled("from then"); } };
promiseResolve(thenable).then(console.log); // "from then"
JAVASCRIPT
const inner = Promise.resolve("ok");
promiseReject(inner).catch((reason) => {
  console.log(reason === inner);          // true: the promise itself is the reason
});
JAVASCRIPT
class Tracked extends Promise {}
const t = Tracked.resolve(5);
promiseResolve(t) === t;                  // false: a new plain Promise that follows t

Environment

  • Plain JavaScript in a browser page; the tests call the globals promiseResolve and promiseReject.
  • You may use new Promise(...). You may not call Promise.resolve or Promise.reject: the tests replace them for a moment to check.
  • this-based calls (promiseResolve.call(SomeClass, x)) and subclasses calling these functions are out of scope.
  • Also out of scope: objects that only pretend to be promises through their prototype, such as Object.create(Promise.prototype). They pass instanceof Promise but are not promises, and the built-in tells them apart with an internal check that plain JavaScript cannot make without side effects; your promiseResolve may return them unchanged.

Hints

  1. Inside new Promise((resolve, reject) => ...), what does resolve already do when you give it a thenable?
  2. instanceof Promise is true for a subclass too. What else tells you the promise came from Promise itself?

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…