UNPKG

850 BJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict
8 */
9
10/**
11 * This function transforms a JS object `ObjMap<Promise<T>>` into
12 * a `Promise<ObjMap<T>>`
13 *
14 * This is akin to bluebird's `Promise.props`, but implemented only using
15 * `Promise.all` so it will work with any implementation of ES6 promises.
16 */
17export default function promiseForObject(object) {
18 var keys = Object.keys(object);
19 var valuesAndPromises = keys.map(function (name) {
20 return object[name];
21 });
22 return Promise.all(valuesAndPromises).then(function (values) {
23 return values.reduce(function (resolvedObject, value, i) {
24 resolvedObject[keys[i]] = value;
25 return resolvedObject;
26 }, Object.create(null));
27 });
28}
\No newline at end of file