Skip to content

Adds an RSVP-like Promise.hash() method for resolving groups of promises.

Notifications You must be signed in to change notification settings

mtimofiiv/promise-hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-hash npm Travis

In the current Promises implementation that Node.js uses, there exists the Promise.all() method, which resolves an array of promises. But what that implementation really misses is the way the rsvp library allows you to pass an object to a promise and retain the data structure of that promise.

This library adds this functionality.

Getting Started

npm install --save promise-hash

In your app, you can then do this:

require('promise-hash');

Because it functions as a polyfill, all you need to do is include this in your code and the Promise object will be polyfilled to include the #hash() method.

If you prefer to not to polyfill the Promise object, you can use it directly:

var hash = require('promise-hash/lib/promise-hash')

Usage Example

require('promise-hash');

function dummyPromise(value) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 100, value);
  });
}

const promises = {
  promise1: dummyPromise('this is the first promise'),
  promise2: dummyPromise('this is the second promise'),
  promise3: dummyPromise('this is the third promise')
};

Promise.hash(promises).then(results => {
  console.log(results); // { promise1: 'this is the first promise', promise2: ... }
});

Nodejs Garbage Collection

If you start getting Promise.hash is not a function, then the culprit is Nodejs's garbage collection. I'm looking into ways of solving this properly (feel free to send me a note if you know a good way!), but in the meantime, try putting the require into an active closure.

About

Adds an RSVP-like Promise.hash() method for resolving groups of promises.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •