API Docs for: 1.0 pre
Show:

Ember.RSVP Class

Module: RSVP

Item Index

Methods

Methods

all

(
  • array
  • label
)
static

This is a convenient alias for RSVP.Promise.all.

Parameters:

  • array Array

    Array of promises.

  • label String

    An optional label. This is useful for tooling.

allSettled

(
  • promises
  • label
)
Promise static

RSVP.allSettled is similar to RSVP.all, but instead of implementing a fail-fast method, it waits until all the promises have returned and shows you all the results. 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. The return promise is fulfilled with an array of the states of the promises passed into the promises array argument.

Each state object will either indicate fulfillment or rejection, and provide the corresponding value or reason. The states will take one of the following formats:

{ state: 'fulfilled', value: value }
  or
{ state: 'rejected', reason: reason }

Example:

var promise1 = RSVP.Promise.resolve(1);
var promise2 = RSVP.Promise.reject(new Error('2'));
var promise3 = RSVP.Promise.reject(new Error('3'));
var promises = [ promise1, promise2, promise3 ];

RSVP.allSettled(promises).then(function(array){
  // array == [
  //   { state: 'fulfilled', value: 1 },
  //   { state: 'rejected', reason: Error },
  //   { state: 'rejected', reason: Error }
  // ]
  // Note that for the second item, reason.message will be "2", and for the
  // third item, reason.message will be "3".
}, function(error) {
  // Not run. (This block would only be called if allSettled had failed,
  // for instance if passed an incorrect argument type.)
});

Parameters:

  • promises Array
  • label String
    • optional string that describes the promise. Useful for tooling.

Returns:

Promise:

promise that is fulfilled with an array of the settled states of the constituent promises.

defer

(
  • label
)
Object

RSVP.defer returns an object similar to jQuery's $.Deferred. RSVP.defer should be used when porting over code reliant on $.Deferred's interface. New code should use the RSVP.Promise constructor instead.

The object returned from RSVP.defer is a plain object with three properties:

  • promise - an RSVP.Promise.
  • reject - a function that causes the promise property on this object to become rejected
  • resolve - a function that causes the promise property on this object to become fulfilled.

Example:

var deferred = RSVP.defer();

deferred.resolve("Success!");

deferred.promise.then(function(value){
  // value here is "Success!"
});

Parameters:

  • label String

    optional string for labeling the promise. Useful for tooling.

Returns:

Object:

denodeify

(
  • nodeFunc
  • binding
)
Function static

RSVP.denodeify takes a "node-style" function and returns a function that will return an RSVP.Promise. You can use denodeify in Node.js or the browser when you'd prefer to use promises over using callbacks. For example, denodeify transforms the following:

var fs = require('fs');

fs.readFile('myfile.txt', function(err, data){
  if (err) return handleError(err);
  handleData(data);
});

into:

var fs = require('fs');

var readFile = RSVP.denodeify(fs.readFile);

readFile('myfile.txt').then(handleData, handleError);

Using denodeify makes it easier to compose asynchronous operations instead of using callbacks. For example, instead of:

var fs = require('fs');
var log = require('some-async-logger');

fs.readFile('myfile.txt', function(err, data){
  if (err) return handleError(err);
  fs.writeFile('myfile2.txt', data, function(err){
    if (err) throw err;
    log('success', function(err) {
      if (err) throw err;
    });
  });
});

You can chain the operations together using then from the returned promise:

var fs = require('fs');
var denodeify = RSVP.denodeify;
var readFile = denodeify(fs.readFile);
var writeFile = denodeify(fs.writeFile);
var log = denodeify(require('some-async-logger'));

readFile('myfile.txt').then(function(data){
  return writeFile('myfile2.txt', data);
}).then(function(){
  return log('SUCCESS');
}).then(function(){
  // success handler
}, function(reason){
  // rejection handler
});

Parameters:

  • nodeFunc Function

    a "node-style" function that takes a callback as its last argument. The callback expects an error to be passed as its first argument (if an error occurred, otherwise null), and the value from the operation as its second argument ("function(err, value){ }").

  • binding Any

    optional argument for binding the "this" value when calling the nodeFunc function.

Returns:

Function:

a function that wraps nodeFunc to return an RSVP.Promise

filter

(
  • promises
  • filterFn
  • label
)
Promise

RSVP.filter is similar to JavaScript's native filter method, except that it waits for all promises to become fulfilled before running the filterFn on each item in given to promises. RSVP.filter returns a promise that will become fulfilled with the result of running filterFn on the values the promises become fulfilled with.

For example:


var promise1 = RSVP.resolve(1);
var promise2 = RSVP.resolve(2);
var promise3 = RSVP.resolve(3);

var filterFn = function(item){
  return item > 1;
};

RSVP.filter(promises, filterFn).then(function(result){
  // result is [ 2, 3 ]
});

If any of the promises given to RSVP.filter are rejected, the first promise that is rejected will be given as an argument to the returned promise's rejection handler. For example:

var promise1 = RSVP.resolve(1);
var promise2 = RSVP.reject(new Error("2"));
var promise3 = RSVP.reject(new Error("3"));
var promises = [ promise1, promise2, promise3 ];

var filterFn = function(item){
  return item > 1;
};

RSVP.filter(promises, filterFn).then(function(array){
  // Code here never runs because there are rejected promises!
}, function(reason) {
  // reason.message === "2"
});

RSVP.filter will also wait for any promises returned from filterFn. For instance, you may want to fetch a list of users then return a subset of those users based on some asynchronous operation:


var alice = { name: 'alice' };
var bob   = { name: 'bob' };
var users = [ alice, bob ];

var promises = users.map(function(user){
  return RSVP.resolve(user);
});

var filterFn = function(user){
  // Here, Alice has permissions to create a blog post, but Bob does not.
  return getPrivilegesForUser(user).then(function(privs){
    return privs.can_create_blog_post === true;
  });
};
RSVP.filter(promises, filterFn).then(function(users){
  // true, because the server told us only Alice can create a blog post.
  users.length === 1;
  // false, because Alice is the only user present in users
  users[0] === bob;
});

Parameters:

  • promises Array
  • filterFn Function
    • function to be called on each resolved value to filter the final results.
  • label String

    optional string describing the promise. Useful for tooling.

Returns:

Promise:

hash

(
  • promises
  • label
)
Promise static

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

Returns a promise that is fulfilled when all the given promises have been fulfilled, or rejected if any of them become rejected. 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 simply be copied over to the fulfilled object.

Example:

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

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

If any of the promises given to RSVP.hash are rejected, the first promise that is rejected will be given as the reason to the rejection handler.

Example:

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

RSVP.hash(promises).then(function(hash){
  // Code here never runs because there are rejected promises!
}, function(reason) {
  // reason.message === "rejectedPromise"
});

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

Example:

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

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

var myObject = new MyConstructor();

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

Parameters:

  • promises Object
  • label String

    optional string that describes the promise. Useful for tooling.

Returns:

Promise:

promise that is fulfilled when all properties of promises have been fulfilled, or rejected if any of them become rejected.

map

(
  • promises
  • mapFn
  • label
)
Promise static

RSVP.map is similar to JavaScript's native map method, except that it waits for all promises to become fulfilled before running the mapFn on each item in given to promises. RSVP.map returns a promise that will become fulfilled with the result of running mapFn on the values the promises become fulfilled with.

For example:


var promise1 = RSVP.resolve(1);
var promise2 = RSVP.resolve(2);
var promise3 = RSVP.resolve(3);
var promises = [ promise1, promise2, promise3 ];

var mapFn = function(item){
  return item + 1;
};

RSVP.map(promises, mapFn).then(function(result){
  // result is [ 2, 3, 4 ]
});

If any of the promises given to RSVP.map are rejected, the first promise that is rejected will be given as an argument to the returned promise's rejection handler. For example:

var promise1 = RSVP.resolve(1);
var promise2 = RSVP.reject(new Error("2"));
var promise3 = RSVP.reject(new Error("3"));
var promises = [ promise1, promise2, promise3 ];

var mapFn = function(item){
  return item + 1;
};

RSVP.map(promises, mapFn).then(function(array){
  // Code here never runs because there are rejected promises!
}, function(reason) {
  // reason.message === "2"
});

RSVP.map will also wait if a promise is returned from mapFn. For example, say you want to get all comments from a set of blog posts, but you need the blog posts first becuase they contain a url to those comments.


var mapFn = function(blogPost){
  // getComments does some ajax and returns an RSVP.Promise that is fulfilled
  // with some comments data
  return getComments(blogPost.comments_url);
};

// getBlogPosts does some ajax and returns an RSVP.Promise that is fulfilled
// with some blog post data
RSVP.map(getBlogPosts(), mapFn).then(function(comments){
  // comments is the result of asking the server for the comments
  // of all blog posts returned from getBlogPosts()
});

Parameters:

  • promises Array
  • mapFn Function

    function to be called on each fulfilled promise.

  • label String

    optional string for labeling the promise. Useful for tooling.

Returns:

Promise:

promise that is fulfilled with the result of calling mapFn on each fulfilled promise or value when they become fulfilled. The promise will be rejected if any of the given promises become rejected.

reject

(
  • reason
  • label
)
Promise static

This is a convenient alias for RSVP.Promise.reject.

Parameters:

  • reason Any

    value that the returned promise will be rejected with.

  • label String

    optional string for identifying the returned promise. Useful for tooling.

Returns:

Promise:

a promise rejected with the given reason.

remove

() public

Provided by the ember module.

Defined in ../packages/metamorph/lib/main.js:130

resolve

(
  • value
  • label
)
Promise static

This is a convenient alias for RSVP.Promise.resolve.

Parameters:

  • value Any

    value that the returned promise will be resolved with

  • label String

    optional string for identifying the returned promise. Useful for tooling.

Returns:

Promise:

a promise that will become fulfilled with the given value

rethrow

(
  • reason
)
static

RSVP.rethrow will rethrow an error on the next turn of the JavaScript event loop in order to aid debugging.

Promises A+ specifies that any exceptions that occur with a promise must be caught by the promises implementation and bubbled to the last handler. For this reason, it is recommended that you always specify a second rejection handler function to then. However, RSVP.rethrow will throw the exception outside of the promise, so it bubbles up to your console if in the browser, or domain/cause uncaught exception in Node. rethrow will also throw the error again so the error can be handled by the promise per the spec.

function throws(){
  throw new Error('Whoops!');
}

var promise = new RSVP.Promise(function(resolve, reject){
  throws();
});

promise.catch(RSVP.rethrow).then(function(){
  // Code here doesn't run because the promise became rejected due to an
  // error!
}, function (err){
  // handle the error here
});

The 'Whoops' error will be thrown on the next turn of the event loop and you can watch for it in your console. You can also handle it using a rejection handler given to .then or .catch on the returned promise.

Parameters:

  • reason Error

    reason the promise became rejected.

Throws:

Error