UNPKG

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