UNPKG

653 BJavaScriptView Raw
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 */
8export 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}