Promise.resolve

The Promise.resolve() static method "resolves" a given value to a Promise. If the value is:

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.

Examples

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

My Solution

Editorial

Solution

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: