promiseForObject.mjs 563 B

123456789101112131415161718
  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 function promiseForObject(object) {
  9. return Promise.all(Object.values(object)).then((resolvedValues) => {
  10. const resolvedObject = Object.create(null);
  11. for (const [i, key] of Object.keys(object).entries()) {
  12. resolvedObject[key] = resolvedValues[i];
  13. }
  14. return resolvedObject;
  15. });
  16. }