Promise.resolve()

Returns a promise resolved with the given value, adopting it if it is already a thenable.

Signature
Promise.resolve(value)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$valueoptionalany= undefinedThe value to resolve the promise with. May also be a Promise or a thenable, in which case the returned promise adopts its state.

Return value

PromiseA Promise resolved with the given value, or the very same promise object if value was already a native Promise. A resolved promise may end up fulfilled, rejected, or still pending — resolving with a rejected promise yields a rejected promise.

Errors

Does not catch synchronous errors thrown while evaluating the argument — those propagate at the call site.

Promise.resolve(value) lifts a plain value into the promise world. The subtlety is in the word resolve, which is not a synonym for fulfill: a resolved promise adopts whatever it was resolved with. Resolve with a rejected promise and you get a rejected promise back.

Identity and flattening

If the argument is already a native promise, you get the same object back — no wrapper is created, so Promise.resolve(p) === p. If it is a thenable from another library, its then() is called and the state is adopted, and nested thenables are flattened all the way down to a single layer.

When to reach for it

Mostly to normalise a function that sometimes has an answer already: return Promise.resolve(cached) on a cache hit so every caller can await the same shape. Also useful for seeding a reduce-built chain, and for turning a foreign thenable into a real promise.

What trips people up

It does not shield you from a synchronous throw. Promise.resolve(risky()) evaluates risky() first, so the error escapes before any promise exists — Promise.try() is what you want there. And the promise still settles asynchronously: handlers run on the microtask queue, never inline.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Return a promise from a cache hit so the caller's code path stays uniform
  • Seed an accumulator when building a sequential chain with reduce
  • Convert a third-party thenable into a real promise
  • Give an async-looking API a synchronous fast path

Frequently asked questions

What is the difference between resolved and fulfilled?

Resolved means the promise's fate has been tied to something — possibly another promise that has not settled yet, or one that rejects. Fulfilled means it settled successfully with a value. Promise.resolve(rejectedPromise) is resolved but rejected.

Does Promise.resolve create a new promise every time?

Not when you pass a native promise — it returns that same instance. For plain values and foreign thenables it creates a new promise.

Can I use Promise.resolve to catch a function's synchronous errors?

No. The argument is evaluated before Promise.resolve is called, so a throw escapes at the call site. Use Promise.try(fn) to bring synchronous throws into the chain.

More examples

Normalising a cached fast path

Both branches return a promise, so callers never have to check whether the answer was cached. The final line shows the mixed batch flowing through Promise.all identically.

Example · javascript
const cache = new Map([["a", 1]]);

const get = (k) =>
  cache.has(k)
    ? Promise.resolve(cache.get(k))
    : new Promise((r) => setTimeout(() => r(k.length), 10));

console.log(await get("a"));
console.log(await get("bcd"));
console.log(await Promise.all(["a", "bcd"].map(get)));

Resolving with a rejected promise

Resolved does not mean fulfilled. Passing a rejected promise returns that exact promise, still rejected, and a thenable that calls its reject callback produces a rejected promise too.

Example · javascript
const rejected = Promise.reject(new Error("still rejected"));
const p = Promise.resolve(rejected);

console.log(p === rejected);
console.log(await p.catch((e) => "caught: " + e.message));

console.log(
  await Promise.resolve({ then(_, rej) { rej(new Error("thenable rejected")); } })
    .catch((e) => e.message),
);

Discussion

  • Be the first to comment on this lesson.