promiseForObject.js 755 B

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