Книга: Angular 2 Cookbook
Назад: Chaining Promises and Promise handlers
Дальше: Implementing Promise barriers with Promise.all()

Creating Promise wrappers with Promise.resolve() and Promise.reject()

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.

Note

The code, links, and a live example of this are available at .

How to do it...

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 normalization

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   

How it works...

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.

There's more...

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   

See also

  • Understanding and implementing basic Promises gives an extensive rundown of how and why to use Promises
  • Implementing Promise barriers with Promise.all() show you how Promises can be composable
  • Canceling asynchronous actions with Promise.race() guides you through the process of implementing a zero-failure-tolerant Promise system
Назад: Chaining Promises and Promise handlers
Дальше: Implementing Promise barriers with Promise.all()

thank you
Flame
cant read the code since it is all on a single line. Also this comments section is russian
Rakuneque
DATA COLLECTION AND ANALYSIS Two reviewers extracted data and assessed methodological quality independently lasix torsemide conversion Many others were in that space already