UNPKG

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