2 module.exports = function(
3 Promise, PromiseArray, tryConvertToPromise, apiRejection) {
4 var util = require("./util");
5 var isObject = util.isObject;
6 var es5 = require("./es5");
8 if (typeof Map === "function") Es6Map = Map;
10 var mapToEntries = (function() {
14 function extractEntry(value, key) {
16 this[index + size] = key;
20 return function mapToEntries(map) {
23 var ret = new Array(map.size * 2);
24 map.forEach(extractEntry, ret);
29 var entriesToMap = function(entries) {
30 var ret = new Es6Map();
31 var length = entries.length / 2 | 0;
32 for (var i = 0; i < length; ++i) {
33 var key = entries[length + i];
34 var value = entries[i];
40 function PropertiesPromiseArray(obj) {
43 if (Es6Map !== undefined && obj instanceof Es6Map) {
44 entries = mapToEntries(obj);
47 var keys = es5.keys(obj);
48 var len = keys.length;
49 entries = new Array(len * 2);
50 for (var i = 0; i < len; ++i) {
52 entries[i] = obj[key];
53 entries[i + len] = key;
56 this.constructor$(entries);
58 this._init$(undefined, -3);
60 util.inherits(PropertiesPromiseArray, PromiseArray);
62 PropertiesPromiseArray.prototype._init = function () {};
64 PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
65 this._values[index] = value;
66 var totalResolved = ++this._totalResolved;
67 if (totalResolved >= this._length) {
70 val = entriesToMap(this._values);
73 var keyOffset = this.length();
74 for (var i = 0, len = this.length(); i < len; ++i) {
75 val[this._values[i + keyOffset]] = this._values[i];
84 PropertiesPromiseArray.prototype.shouldCopyValues = function () {
88 PropertiesPromiseArray.prototype.getActualLength = function (len) {
92 function props(promises) {
94 var castValue = tryConvertToPromise(promises);
96 if (!isObject(castValue)) {
97 return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a");
98 } else if (castValue instanceof Promise) {
99 ret = castValue._then(
100 Promise.props, undefined, undefined, undefined, undefined);
102 ret = new PropertiesPromiseArray(castValue).promise();
105 if (castValue instanceof Promise) {
106 ret._propagateFrom(castValue, 2);
111 Promise.prototype.props = function () {
115 Promise.props = function (promises) {
116 return props(promises);