promiseForObject.js 683 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.promiseForObject = 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. return Promise.all(Object.values(object)).then((resolvedValues) => {
  15. const resolvedObject = Object.create(null);
  16. for (const [i, key] of Object.keys(object).entries()) {
  17. resolvedObject[key] = resolvedValues[i];
  18. }
  19. return resolvedObject;
  20. });
  21. }