It is useful to have the ability to create promise objects that have already reached a final state with a defined value, and also to be able to normalize JavaScript objects into promises. Promise.resolve()
and Promise.reject()
afford you the ability to perform both these actions.
The code, links, and a live example of this are available at .
Like all other static Promise methods, Promise.resolve()
and Promise.reject()
return a promise object. In this case, there is no executor
definition.
If one of these methods is provided with a non-promise argument, the returned promise will assume either a fulfilled
or rejected
state (corresponding to the invoked method). This method will pass the argument to Promise.resolve()
and Promise.reject()
, along with any corresponding handlers:
Promise.resolve('foo'); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "foo"} Promise.reject('bar'); // Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: "bar"} // (error) Uncaught (in promise) bar
The preceding code is behaviorally equivalent to the following:
new Promise((resolve, reject) => resolve('foo')); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "foo"} new Promise((resolve, reject) => reject ('bar')); >> Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: "bar"} // (error) Uncaught (in promise) bar
Promise.resolve()
will uniquely handle scenarios where it is passed with a promise object as its argument. Promise.resolve()
will effectively operate as a no-op, returning the initial promise argument without any modification. It will not make an attempt to coerce the argument promise's state:
const a = Promise.resolve('baz'); console.log(a); // Promise {status: 'resolved', value: 'baz'} const b = Promise.resolve(a); console.log(b); // Promise {status: 'resolved', value: 'baz'} console.log(a === b); // true const c = Promise.reject('qux'); // Error qux console.log(c) // Promise {status: 'rejected', value: 'qux'} const d = Promise.resolve(c); console.log(d); // Promise {status: 'rejected', value: 'qux'} console.log(c === d); // true
When thinking about Promises
in the context of them "promising" to eventually assume a value, these methods are simply ameliorating any latent period separating the pending and final states.
The dichotomy is very simple:
Promise.reject()
will return a rejected promise no matter what its argument is. Even if it is a promise object, the value of the returned promise will be that of the promise object.Promise.resolve()
will return a fulfilled promise with the wrapped value if that value is not a promise. If it is a promise, it behaves as a no-op.Importantly, the behavior of Promise.resolve()
is nearly the same as how $q.when()
operated in Angular 1. $q.when()
was able to normalize promise objects, but it would always return a newly created promise object:
// Angular 1 const a = $q(() => {}); console.log(a); // Promise {...} const b = $q.when(a); console.log(b); // Promise {...} console.log(a === b); // false