Promise.resolve()
The Promise.resolve()
method "resolves" a given value to a Promise
. If the value is a promise, that promise is returned; if the value is a thenable, Promise.resolve()
will call the then()
method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.
This function flattens nested layers of promise-like objects (e.g. a promise that fulfills to a promise that fulfills to something) into a single layer — a promise that fulfills to a non-thenable value.
Syntax
Promise.resolve(value)
Parameters
value
- : Argument to be resolved by this
Promise
. Can also be aPromise
or a thenable to resolve.
- : Argument to be resolved by this
Return value
A Promise
that is resolved with the given value, or the promise passed as value, if the value was a promise object. A resolved promise can be in any of the states — fulfilled, rejected, or pending. For example, resolving a rejected promise will still result in a rejected promise.
Description
Promise.resolve()
resolves a promise, which is not the same as fulfilling or rejecting the promise. See Promise description for definitions of the terminology. In brief, Promise.resolve()
returns a promise whose eventual state depends on another promise, thenable object, or other value.
Promise.resolve()
is generic and supports subclassing, which means it can be called on subclasses of Promise
, and the result will be a promise of the subclass type. To do so, the subclass's constructor must implement the same signature as the Promise()
constructor — accepting a single executor
function that can be called with the resolve
and reject
callbacks as parameters.
Promise.resolve()
special-cases native Promise
instances. If value
belongs to Promise
or a subclass, and value.constructor === Promise
, then value
is directly returned by Promise.resolve()
, without creating a new Promise
instance. Otherwise, Promise.resolve()
is essentially a shorthand for new Promise((resolve) => resolve(value))
.
The bulk of the resolving logic is actually implemented by the resolver function passed by the Promise()
constructor. In summary:
- If a non-thenable value is passed, the returned promise is already fulfilled with that value.
- If a thenable is passed, the returned promise will adopt the state of that thenable by calling the
then
method and passing a pair of resolving functions as arguments. (But because native promises directly pass throughPromise.resolve()
without creating a wrapper, thethen
method is not called on native promises.) If the resolver function receives another thenable object, it will be resolved agin, so that the eventual fulfillment value of the promise will never be thenable.