The Promise.resolve() static method "resolves" a given value to a Promise. If the value is:
Promise.resolve() will call the then() method and pass a pair of resolving functions as arguments. A promise that has the same state as the thenable is returned.Source: Promise.resolve() - JavaScript | MDN
A "thenable" is an interface that implements the .then() method, which is called with two callbacks: one for when the promise is fulfilled, one for when it's rejected. Promises are also thenables.
Implement the Promise.resolve() function as promiseResolve. You can ignore the case where this is referenced within the implemented function.
Resolving a non-promise.
const p = promiseResolve(42);
await p; // 42
Resolving a Promise.
const original = new Promise((resolve) => resolve(42));
const cast = promiseResolve(original);
await cast; // 42
Resolving a thenable.
const resolvedThenable = promiseResolve({
then(resolve, reject) {
resolve(42);
},
});
await resolvedThenable; // 42
The purpose of Promise.resolve is to add a safe wrapper around any value such that can be used with then() or await-ed.
There are three cases to handle within the static Promise.resolve function: