Skip to content
jshacks.dev
Async

Promise.resolve and Promise.reject

Learn what Promise.resolve and Promise.reject do in JavaScript, how they treat thenables and existing promises, and where they help in tests and APIs.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, Vite, npm and TypeScript logos.

Two one-line static methods do more work than their size suggests. Promise.resolve(value) returns a promise fulfilled with value, unless value is already a promise, in which case it returns that very promise. Promise.reject(reason) returns a rejected promise with no such special treatment. That asymmetry is the whole subject, and it explains why Promise.resolve is the standard way to normalize "a value or a promise" into one shape and why Promise.reject(somePromise) does not do what the name suggests. This post covers both on Node.js 24 with printed output: identity behavior, thenable adoption, the uses that earn their keep and the one habit worth dropping.

Promise.resolve with a plain value

Passing any non-promise value returns a promise already fulfilled with it.

JavaScript
const promise = Promise.resolve(42);

console.log(promise);
promise.then((value) => console.log("value:", value));
Text
Promise { 42 }
value: 42

The promise is settled the moment it is created, but the then handler still runs asynchronously, as a microtask, after the current code. That is the same ordering guarantee every promise gives, covered in the post on promise states.

Promise.resolve with a promise returns the same promise

If you pass a native promise, you get the same object back, not a wrapper.

JavaScript
const original = Promise.resolve("x");
const wrapped = Promise.resolve(original);

console.log(original === wrapped);
Text
true

This identity rule is deliberate. It avoids a layer of promise for every call, so code can call Promise.resolve(maybePromise) freely. It holds only when the constructor matches. A promise from another realm or a subclass instance goes through the adoption path instead, and Promise.resolve builds a new one. For everyday code, treat it as "same promise".

Promise.resolve with a thenable follows it

An object with a then method is adopted. The returned promise settles however the thenable does.

JavaScript
const thenable = {
  then(onFulfilled) {
    setTimeout(() => onFulfilled("from the thenable"), 10);
  },
};

const promise = Promise.resolve(thenable);

console.log(promise instanceof Promise);
console.log(await promise);
Text
true
from the thenable

The call returned a real promise immediately, which settled ten milliseconds later. This is how code can accept promises from older libraries, and it is why await works on thenables too. One caution: adoption calls the object's then, so an object that happens to have a then property that is not a promise-like function can surprise you.

Promise.reject never unwraps

Promise.reject(reason) always makes a new rejected promise whose reason is exactly what you passed, even if that is itself a promise.

JavaScript
const inner = Promise.resolve("inner value");
const rejected = Promise.reject(inner);

rejected.catch((reason) => {
  console.log(reason === inner);
  console.log(reason);
});
Text
true
Promise { 'inner value' }

The reason is the promise object, not its value. This is the asymmetry. resolve unwraps and adopts, while reject stores whatever it receives. A handler written as catch((error) => error.message) would fail here, since the reason has no message. By convention, reject with an Error object, so it carries a stack trace.

JavaScript
Promise.reject(new Error("lookup failed")).catch((error) => {
  console.log(error instanceof Error, error.message);
});
Text
true lookup failed

Unhandled rejections

A rejected promise nobody handles is reported when the microtask queue drains. On current Node.js versions, the default behavior for an unhandled rejection is to throw it as an uncaught exception, which ends the process.

JavaScript
process.on("unhandledRejection", (reason) => {
  console.log("unhandled:", reason.message);
});

Promise.reject(new Error("nobody listens"));
Text
unhandled: nobody listens

Registering a handler like this is for logging at the edge of a program. The real fix is to attach catch or await inside try. Creating a rejected promise just to return it is fine as long as the caller handles it.

Where Promise.resolve earns its place

Normalizing a value or a promise. An API that accepts either a result or a function returning a promise can pass everything through Promise.resolve.

JavaScript
function load(source) {
  return Promise.resolve(typeof source === "function" ? source() : source);
}

console.log(await load(5));
console.log(await load(() => Promise.resolve(6)));
console.log(await load(() => 7));
Text
5
6
7

Starting a chain. Promise.resolve().then(...) runs a function in a microtask and turns a throw into a rejection. For this specific job, Promise.try is cleaner on runtimes that have it, as covered in its post.

Returning an already known result from a function that is otherwise asynchronous, such as a cache hit, so callers always receive a promise.

JavaScript
const cache = new Map([["a", "cached"]]);

function getValue(key) {
  if (cache.has(key)) return Promise.resolve(cache.get(key));
  return new Promise((resolve) => setTimeout(() => resolve("fetched"), 10));
}

console.log(await getValue("a"));
console.log(await getValue("b"));
Text
cached
fetched

Both branches return a promise, so the caller never needs to check. In an async function, you do not need Promise.resolve for this at all, since a returned value is wrapped automatically.

Where Promise.reject earns its place

It is mostly used for early exits in functions that must return a promise but are not async.

JavaScript
function parsePort(text) {
  const port = Number(text);

  if (!Number.isInteger(port) || port < 1 || port > 65535) {
    return Promise.reject(new RangeError(`invalid port: ${text}`));
  }

  return Promise.resolve(port);
}

await parsePort("8080").then((p) => console.log("port", p));
await parsePort("99999").catch((e) => console.log(e.name, e.message));
Text
port 8080
RangeError invalid port: 99999

Inside an async function, write throw new RangeError(...) instead. It produces the same rejected promise with less ceremony. Reach for Promise.reject in plain functions, and in tests where you need a failing stub.

In tests

Both methods are the quickest way to stub an asynchronous dependency.

JavaScript
const okStub = () => Promise.resolve({ id: 1 });
const failStub = () => Promise.reject(new Error("db down"));

console.log(await okStub());

try {
  await failStub();
} catch (error) {
  console.log("handled:", error.message);
}
Text
{ id: 1 }
handled: db down

The habit to drop

Wrapping a value that is already a promise, new Promise((resolve) => resolve(somePromise)), adds nothing. The constructor form is for callback APIs. For a value or a promise, use Promise.resolve, and in an async function, just return it.

How these methods show up in real code

In day-to-day work you will call Promise.resolve more often than Promise.reject, and mostly in the same few situations. One is a library function with a flexible signature that accepts a value, a promise or a function returning either. Another is a test double that stands in for a network call. A third is a cache or memoized function that must keep returning a promise even when the answer is already known, so that its callers never have to branch on the type of the result.

The reason to keep the return type consistent is subtle but important. A function that sometimes returns a promise and sometimes a plain value forces every caller to handle both, and callers that forget will work in the cached case and fail in the uncached one, which is exactly the kind of bug that appears only in production. Returning a promise every time costs a microtask hop and removes the bug. In an async function you get that for free, and in a plain function Promise.resolve is the way to get it.

Finally, remember that rejection is for exceptional outcomes. A lookup that finds nothing is usually a normal result and should fulfill with null or an empty value, and not reject. Reserve rejection for failures the caller must handle differently from success.

Testing code that accepts values or promises

When a function takes either a value or a promise, test both inputs with the same assertions, so the contract is clear. A small table of cases, a plain number, a fulfilled promise, a thenable, a function returning a promise, each expected to give the same result after await, covers the normalization. Add a case for a rejected promise and one for a function that throws synchronously, and assert that both arrive as rejections and not as thrown exceptions. If the function should return the same promise it was given, assert identity with Promise.resolve(p) === p. Finally, test that the function never calls its handler synchronously, by recording the order of a log entry made right after the call. That ordering guarantee is part of the contract, and a refactor that breaks it is easy to make and hard to spot in review.

Share

Keep reading

Flat illustration of an editor window full of code, with the Google Chrome, JavaScript and npm logos.
Browser

script Tags: async, defer and type=module

Compare classic, async, defer and module script tags: when each downloads, when each runs, how order is kept and which one to pick for each kind of script.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.