13.1. Promise.resolve and Promise.reject
Topics
This question is in
13. Promise from scratchCode 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
function promiseResolve(value) // returns a Promise
function promiseReject(reason) // returns a Promisevalue: anything: a plain value, a promise, or a thenable (any object or function with athenmethod).reason: anything, kept exactly as it is.- Both return a real
Promise(result instanceof Promiseis true).
Requirements
promiseResolvealways returns aPromise. A plain value (a number,undefined,null, an object) becomes the fulfilled value, the very same object, not a copy.- A promise made by
Promiseitself (instanceof Promise, and itsconstructorisPromise) is returned unchanged: the same object, pending or settled. A thenable that only has aconstructorproperty set toPromiseis not a promise: it is adopted (requirement 4). - Any other promise, such as one from a subclass of
Promise, or one whoseconstructorproperty was changed, gives a new plainPromisethat follows it. - A thenable is adopted: the returned promise fulfills or rejects as the thenable's
thensays. - The thenable's
thenis called later, in a microtask, with the thenable asthis; never during the call topromiseResolve. - If reading
thenthrows, orthenthrows before it settles anything, the promise rejects with that error. A throw after it has settled changes nothing. - An object whose
thenis not a function is just a value. promiseRejectreturns a new promise rejected withreasonexactly: a promise or a thenable given as the reason is never unwrapped, and never returned itself.- Neither function calls
Promise.resolveorPromise.reject.
Examples
const p = Promise.resolve(1);
promiseResolve(p) === p; // true: the same promise
promiseResolve(42).then(console.log); // 42const thenable = { then(onFulfilled) { onFulfilled("from then"); } };
promiseResolve(thenable).then(console.log); // "from then"const inner = Promise.resolve("ok");
promiseReject(inner).catch((reason) => {
console.log(reason === inner); // true: the promise itself is the reason
});class Tracked extends Promise {}
const t = Tracked.resolve(5);
promiseResolve(t) === t; // false: a new plain Promise that follows tEnvironment
- Plain JavaScript in a browser page; the tests call the globals
promiseResolveandpromiseReject. - You may use
new Promise(...). You may not callPromise.resolveorPromise.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 passinstanceof Promisebut are not promises, and the built-in tells them apart with an internal check that plain JavaScript cannot make without side effects; yourpromiseResolvemay return them unchanged.
Hints
- Inside
new Promise((resolve, reject) => ...), what doesresolvealready do when you give it a thenable? instanceof Promiseis true for a subclass too. What else tells you the promise came fromPromiseitself?
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