RSVP.hashSettled()

hashSettled (object, label) Promisestatic

Defined in bower_components/rsvp/lib/rsvp/hash-settled.js:23

RSVP.hashSettled is similar to RSVP.allSettled, but takes an object instead of an array for its promises argument.

Unlike RSVP.all or RSVP.hash, which implement a fail-fast method, but like RSVP.allSettled, hashSettled waits until all the constituent promises have returned and then shows you all the results with their states and values/reasons. This is useful if you want to handle multiple promises' failure states together as a set.

Returns a promise that is fulfilled when all the given promises have been settled, or rejected if the passed parameters are invalid.

The returned promise is fulfilled with a hash that has the same key names as the promises object argument. If any of the values in the object are not promises, they will be copied over to the fulfilled object and marked with state 'fulfilled'.

Example:

var promises = {
  myPromise: RSVP.Promise.resolve(1),
  yourPromise: RSVP.Promise.resolve(2),
  theirPromise: RSVP.Promise.resolve(3),
  notAPromise: 4
};

RSVP.hashSettled(promises).then(function(hash){
  // hash here is an object that looks like:
  // {
  //   myPromise: { state: 'fulfilled', value: 1 },
  //   yourPromise: { state: 'fulfilled', value: 2 },
  //   theirPromise: { state: 'fulfilled', value: 3 },
  //   notAPromise: { state: 'fulfilled', value: 4 }
  // }
});

If any of the promises given to RSVP.hash are rejected, the state will be set to 'rejected' and the reason for rejection provided.

Example:

var promises = {
  myPromise: RSVP.Promise.resolve(1),
  rejectedPromise: RSVP.Promise.reject(new Error('rejection')),
  anotherRejectedPromise: RSVP.Promise.reject(new Error('more rejection')),
};

RSVP.hashSettled(promises).then(function(hash){
  // hash here is an object that looks like:
  // {
  //   myPromise:              { state: 'fulfilled', value: 1 },
  //   rejectedPromise:        { state: 'rejected', reason: Error },
  //   anotherRejectedPromise: { state: 'rejected', reason: Error },
  // }
  // Note that for rejectedPromise, reason.message == 'rejection',
  // and for anotherRejectedPromise, reason.message == 'more rejection'.
});

An important note: RSVP.hashSettled is intended for plain JavaScript objects that are just a set of keys and values. RSVP.hashSettled will NOT preserve prototype chains.

Example:

function MyConstructor(){
  this.example = RSVP.Promise.resolve('Example');
}

MyConstructor.prototype = {
  protoProperty: RSVP.Promise.resolve('Proto Property')
};

var myObject = new MyConstructor();

RSVP.hashSettled(myObject).then(function(hash){
  // protoProperty will not be present, instead you will just have an
  // object that looks like:
  // {
  //   example: { state: 'fulfilled', value: 'Example' }
  // }
  //
  // hash.hasOwnProperty('protoProperty'); // false
  // 'undefined' === typeof hash.protoProperty
});

Parameters:

object Object
label String
optional string that describes the promise. Useful for tooling.

Returns:

Promise
promise that is fulfilled when when all properties of `promises` have been settled.
doc_EmberJs
2016-11-30 16:53:24
Comments
Leave a Comment

Please login to continue.