Class rsvp
all (array, label) public
Defined in node_modules/rsvp/lib/rsvp/all.js:3
import { all } from 'rsvp'; |
- array
- Array
- Array of promises.
- label
- String
- An optional label. This is useful for tooling.
This is a convenient alias for Promise.all
.
allSettled (entries, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/all-settled.js:20
import { allSettled } from 'rsvp'; |
- entries
- 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.
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:
1 2 3 |
{ state: 'fulfilled', value: value } or { state: 'rejected', reason: reason } |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let promise1 = RSVP.Promise.resolve(1); let promise2 = RSVP.Promise.reject(new Error('2')); let promise3 = RSVP.Promise.reject(new Error('3')); let 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.) }); |
defer (label) Object public
Defined in node_modules/rsvp/lib/rsvp/defer.js:3
import { defer } from 'rsvp'; |
- label
- String
- optional string for labeling the promise. Useful for tooling.
- returns
- Object
defer
returns an object similar to jQuery's $.Deferred
.
defer
should be used when porting over code reliant on $.Deferred
's
interface. New code should use the Promise
constructor instead.
The object returned from defer
is a plain object with three properties:
- promise - an
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:
1 2 3 4 5 6 7 |
let deferred = defer(); deferred.resolve("Success!"); deferred.promise.then(function(value){ // value here is "Success!" }); |
denodeify (nodeFunc, options) Function public
Defined in node_modules/rsvp/lib/rsvp/node.js:44
import { denodeify } from 'rsvp'; |
- 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){ }').
- options
- Boolean|Array
- An optional paramter that if set to `true` causes the promise to fulfill with the callback's success arguments as an array. This is useful if the node function has multiple success paramters. If you set this paramter to an array with names, the promise will fulfill with a hash with these names as keys and the success parameters as values.
- returns
- Function
- a function that wraps `nodeFunc` to return a `Promise`
denodeify
takes a 'node-style' function and returns a function that
will return an 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:
1 2 3 4 5 6 |
let fs = require('fs'); fs.readFile('myfile.txt', function(err, data){ if (err) return handleError(err); handleData(data); }); |
into:
1 2 3 4 |
let fs = require('fs'); let readFile = denodeify(fs.readFile); readFile('myfile.txt').then(handleData, handleError); |
If the node function has multiple success parameters, then denodeify
just returns the first one:
1 2 3 4 5 |
let request = denodeify(require('request')); request('http://example.com').then(function(res) { // ... }); |
However, if you need all success parameters, setting denodeify
's
second parameter to true
causes it to return all success parameters
as an array:
1 2 3 4 5 6 |
let request = denodeify(require('request'), true); request('http://example.com').then(function(result) { // result[0] -> res // result[1] -> body }); |
Or if you pass it an array with names it returns the parameters as a hash:
1 2 3 4 5 6 |
let request = denodeify(require('request'), ['res', 'body']); request('http://example.com').then(function(result) { // result.res // result.body }); |
Sometimes you need to retain the this
:
1 2 |
let app = require('express')(); let render = denodeify(app.render.bind(app)); |
The denodified function inherits from the original function. It works in all environments, except IE 10 and below. Consequently all properties of the original function are available to you. However, any properties you change on the denodeified function won't be changed on the original function. Example:
1 2 3 4 5 6 |
let request = denodeify(require('request')), cookieJar = request.jar(); // <- Inheritance is used here request('http://example.com', {jar: cookieJar}).then(function(res) { // cookieJar.cookies holds now the cookies returned by example.com }); |
Using denodeify
makes it easier to compose asynchronous operations instead
of using callbacks. For example, instead of:
1 2 3 4 5 6 7 8 9 |
let fs = require('fs'); fs.readFile('myfile.txt', function(err, data){ if (err) { ... } // Handle error fs.writeFile('myfile2.txt', data, function(err){ if (err) { ... } // Handle error console.log('done') }); }); |
you can chain the operations together using then
from the returned promise:
1 2 3 4 5 6 7 8 9 10 11 |
let fs = require('fs'); let readFile = denodeify(fs.readFile); let writeFile = denodeify(fs.writeFile); readFile('myfile.txt').then(function(data){ return writeFile('myfile2.txt', data); }).then(function(){ console.log('done') }).catch(function(error){ // Handle error }); |
filter (promises, filterFn, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/filter.js:42
import { filter } from 'rsvp'; |
- 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
filter
is similar to JavaScript's native filter
method.
filterFn
is eagerly called meaning that as soon as any promise
resolves its value will be passed to filterFn
. filter
returns
a promise that will become fulfilled with the result of running
filterFn
on the values the promises become fulfilled with.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import { filter, resolve } from 'rsvp'; let promise1 = resolve(1); let promise2 = resolve(2); let promise3 = resolve(3); let promises = [promise1, promise2, promise3]; let filterFn = function(item){ return item > 1; }; filter(promises, filterFn).then(function(result){ // result is [ 2, 3 ] }); |
If any of the promises
given to filter
are rejected, the first promise
that is rejected will be given as an argument to the returned promise's
rejection handler. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { filter, reject, resolve } from 'rsvp'; let promise1 = resolve(1); let promise2 = reject(new Error('2')); let promise3 = reject(new Error('3')); let promises = [ promise1, promise2, promise3 ]; let filterFn = function(item){ return item > 1; }; filter(promises, filterFn).then(function(array){ // Code here never runs because there are rejected promises! }, function(reason) { // reason.message === '2' }); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import { filter, resolve } from 'rsvp'; let alice = { name: 'alice' }; let bob = { name: 'bob' }; let users = [ alice, bob ]; let promises = users.map(function(user){ return resolve(user); }); let 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; }); }; 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; }); |
hash (object, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/hash.js:4
import { hash } from 'rsvp'; |
- object
- 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.
hash
is similar to 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
let promises = { myPromise: resolve(1), yourPromise: resolve(2), theirPromise: resolve(3), notAPromise: 4 }; 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 hash
are rejected, the first promise
that is rejected will be given as the reason to the rejection handler.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
let promises = { myPromise: resolve(1), rejectedPromise: reject(new Error('rejectedPromise')), anotherRejectedPromise: reject(new Error('anotherRejectedPromise')), }; hash(promises).then(function(hash){ // Code here never runs because there are rejected promises! }, function(reason) { // reason.message === 'rejectedPromise' }); |
An important note: hash
is intended for plain JavaScript objects that
are just a set of keys and values. hash
will NOT preserve prototype
chains.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import { hash, resolve } from 'rsvp'; function MyConstructor(){ this.example = resolve('Example'); } MyConstructor.prototype = { protoProperty: resolve('Proto Property') }; let myObject = new MyConstructor(); 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 }); |
hashSettled (object, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/hash-settled.js:16
import { hashSettled } from 'rsvp'; |
- 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.
hashSettled
is similar to allSettled
, but takes an object
instead of an array for its promises
argument.
Unlike all
or hash
, which implement a fail-fast method,
but like 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import { hashSettled, resolve } from 'rsvp'; let promises = { myPromise: resolve(1), yourPromise: resolve(2), theirPromise: resolve(3), notAPromise: 4 }; 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 hash
are rejected, the state will
be set to 'rejected' and the reason for rejection provided.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import { hashSettled, reject, resolve } from 'rsvp'; let promises = { myPromise: resolve(1), rejectedPromise: reject(new Error('rejection')), anotherRejectedPromise: reject(new Error('more rejection')), }; 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: hashSettled
is intended for plain JavaScript objects that
are just a set of keys and values. hashSettled
will NOT preserve prototype
chains.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import Promise, { hashSettled, resolve } from 'rsvp'; function MyConstructor(){ this.example = resolve('Example'); } MyConstructor.prototype = { protoProperty: Promise.resolve('Proto Property') }; let myObject = new MyConstructor(); 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 }); |
map (promises, mapFn, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/map.js:39
import { map } from 'rsvp'; |
- 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.
map
is similar to JavaScript's native map
method. mapFn
is eagerly called
meaning that as soon as any promise resolves its value will be passed to mapFn
.
map
returns a promise that will become fulfilled with the result of running
mapFn
on the values the promises become fulfilled with.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { map, resolve } from 'rsvp'; let promise1 = resolve(1); let promise2 = resolve(2); let promise3 = resolve(3); let promises = [ promise1, promise2, promise3 ]; let mapFn = function(item){ return item + 1; }; map(promises, mapFn).then(function(result){ // result is [ 2, 3, 4 ] }); |
If any of the promises
given to map
are rejected, the first promise
that is rejected will be given as an argument to the returned promise's
rejection handler. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { map, reject, resolve } from 'rsvp'; let promise1 = resolve(1); let promise2 = reject(new Error('2')); let promise3 = reject(new Error('3')); let promises = [ promise1, promise2, promise3 ]; let mapFn = function(item){ return item + 1; }; map(promises, mapFn).then(function(array){ // Code here never runs because there are rejected promises! }, function(reason) { // reason.message === '2' }); |
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 because they contain a url to those comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { map } from 'rsvp'; let mapFn = function(blogPost){ // getComments does some ajax and returns an Promise that is fulfilled // with some comments data return getComments(blogPost.comments_url); }; // getBlogPosts does some ajax and returns an Promise that is fulfilled // with some blog post data map(getBlogPosts(), mapFn).then(function(comments){ // comments is the result of asking the server for the comments // of all blog posts returned from getBlogPosts() }); |
race (array, label) public
Defined in node_modules/rsvp/lib/rsvp/race.js:3
import { race } from 'rsvp'; |
- array
- Array
- Array of promises.
- label
- String
- An optional label. This is useful for tooling.
This is a convenient alias for Promise.race
.
reject (reason, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/reject.js:3
import { reject } from 'rsvp'; |
- reason
- *
- 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`.
This is a convenient alias for Promise.reject
.
resolve (value, label) Promise public
Defined in node_modules/rsvp/lib/rsvp/resolve.js:3
import { resolve } from 'rsvp'; |
- value
- *
- 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`
This is a convenient alias for Promise.resolve
.
rethrow (reason) public
Defined in node_modules/rsvp/lib/rsvp/rethrow.js:1
- reason
- Error
- reason the promise became rejected.
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, 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { rethrow } from 'rsvp'; function throws(){ throw new Error('Whoops!'); } let promise = new Promise(function(resolve, reject){ throws(); }); promise.catch(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.