promiseForObject.mjs 653 B

12345678910111213141516171819
  1. /**
  2. * This function transforms a JS object `ObjMap<Promise<T>>` into
  3. * a `Promise<ObjMap<T>>`
  4. *
  5. * This is akin to bluebird's `Promise.props`, but implemented only using
  6. * `Promise.all` so it will work with any implementation of ES6 promises.
  7. */
  8. export default function promiseForObject(object) {
  9. var keys = Object.keys(object);
  10. var valuesAndPromises = keys.map(function (name) {
  11. return object[name];
  12. });
  13. return Promise.all(valuesAndPromises).then(function (values) {
  14. return values.reduce(function (resolvedObject, value, i) {
  15. resolvedObject[keys[i]] = value;
  16. return resolvedObject;
  17. }, Object.create(null));
  18. });
  19. }