JS: Promise.resolve

By Xah Lee. Date: . Last updated: .
Promise.resolve(x)

return a promise fulfilled with x, except when:

  • if x is a promise, just return it.
  • if x is a thenable, return a promise synced to its state.

when x is not a special value above, Promise.resolve(x) is effectively new Promise((aa) => { aa(x); })

Example. normal resolve

Promise.resolve(42)
 .then(
  (x) => console.log("fulfilled with value:", x),
  (x) => console.log("rejected with value:", x),
 );
// fulfilled with value: 42

Example. arg is a promise

// example showing Promise.resolve(x) returns x if x is a promise

const xa = Promise.reject("gone bad.");
const xb = Promise.resolve(xa);

console.log(xa === xb);
// true

xb.then(
 (x) => console.log("Fulfilled with value:", x),
 (x) => console.log("Rejected with value:", x),
);
// Rejected with value: gone bad.