react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / promise_array.js
81160 views"use strict";1module.exports = function(Promise, INTERNAL, tryConvertToPromise,2apiRejection) {3var util = require("./util.js");4var isArray = util.isArray;56function toResolutionValue(val) {7switch(val) {8case -2: return [];9case -3: return {};10}11}1213function PromiseArray(values) {14var promise = this._promise = new Promise(INTERNAL);15var parent;16if (values instanceof Promise) {17parent = values;18promise._propagateFrom(parent, 1 | 4);19}20this._values = values;21this._length = 0;22this._totalResolved = 0;23this._init(undefined, -2);24}25PromiseArray.prototype.length = function () {26return this._length;27};2829PromiseArray.prototype.promise = function () {30return this._promise;31};3233PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {34var values = tryConvertToPromise(this._values, this._promise);35if (values instanceof Promise) {36values = values._target();37this._values = values;38if (values._isFulfilled()) {39values = values._value();40if (!isArray(values)) {41var err = new Promise.TypeError("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");42this.__hardReject__(err);43return;44}45} else if (values._isPending()) {46values._then(47init,48this._reject,49undefined,50this,51resolveValueIfEmpty52);53return;54} else {55this._reject(values._reason());56return;57}58} else if (!isArray(values)) {59this._promise._reject(apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a")._reason());60return;61}6263if (values.length === 0) {64if (resolveValueIfEmpty === -5) {65this._resolveEmptyArray();66}67else {68this._resolve(toResolutionValue(resolveValueIfEmpty));69}70return;71}72var len = this.getActualLength(values.length);73this._length = len;74this._values = this.shouldCopyValues() ? new Array(len) : this._values;75var promise = this._promise;76for (var i = 0; i < len; ++i) {77var isResolved = this._isResolved();78var maybePromise = tryConvertToPromise(values[i], promise);79if (maybePromise instanceof Promise) {80maybePromise = maybePromise._target();81if (isResolved) {82maybePromise._unsetRejectionIsUnhandled();83} else if (maybePromise._isPending()) {84maybePromise._proxyPromiseArray(this, i);85} else if (maybePromise._isFulfilled()) {86this._promiseFulfilled(maybePromise._value(), i);87} else {88this._promiseRejected(maybePromise._reason(), i);89}90} else if (!isResolved) {91this._promiseFulfilled(maybePromise, i);92}93}94};9596PromiseArray.prototype._isResolved = function () {97return this._values === null;98};99100PromiseArray.prototype._resolve = function (value) {101this._values = null;102this._promise._fulfill(value);103};104105PromiseArray.prototype.__hardReject__ =106PromiseArray.prototype._reject = function (reason) {107this._values = null;108this._promise._rejectCallback(reason, false, true);109};110111PromiseArray.prototype._promiseProgressed = function (progressValue, index) {112this._promise._progress({113index: index,114value: progressValue115});116};117118119PromiseArray.prototype._promiseFulfilled = function (value, index) {120this._values[index] = value;121var totalResolved = ++this._totalResolved;122if (totalResolved >= this._length) {123this._resolve(this._values);124}125};126127PromiseArray.prototype._promiseRejected = function (reason, index) {128this._totalResolved++;129this._reject(reason);130};131132PromiseArray.prototype.shouldCopyValues = function () {133return true;134};135136PromiseArray.prototype.getActualLength = function (len) {137return len;138};139140return PromiseArray;141};142143144