creation

Promise.resolve

Returns a promise fulfilled with the given value, or — if the value is already a promise or thenable — the same/adopted promise. Handy for normalizing 'might be a promise' inputs.

Promise.resolve(value)

Parameters

Parameter Purpose
value Any value — plain or thenable
thenable adoption If value is a thenable, Promise.resolve follows its state
same-promise If value is already a native Promise, the same instance is returned

Examples

const p = Promise.resolve(42);
p.then(v => console.log(v)); // 42

Immediately fulfilled with 42.

function loadCached(key) {
  return cache.has(key)
    ? Promise.resolve(cache.get(key))
    : fetch(key).then(r => r.json());
}

Return a resolved promise to keep the API async-uniform.

const inner = new Promise(res => setTimeout(() => res('x'), 100));
const outer = Promise.resolve(inner);
console.log(outer === inner); // true (adopts, doesn't wrap)

Passing a native promise returns the same instance.

Gotcha

Promise.resolve(thenable) follows the thenable — if that thenable rejects, so does yours. Cannot cancel or replace the value once created.

Related

← All JS Promise reference · async/await vs promises vs callbacks