react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / some.js
81160 views"use strict";1module.exports =2function(Promise, PromiseArray, apiRejection) {3var util = require("./util.js");4var RangeError = require("./errors.js").RangeError;5var AggregateError = require("./errors.js").AggregateError;6var isArray = util.isArray;789function SomePromiseArray(values) {10this.constructor$(values);11this._howMany = 0;12this._unwrap = false;13this._initialized = false;14}15util.inherits(SomePromiseArray, PromiseArray);1617SomePromiseArray.prototype._init = function () {18if (!this._initialized) {19return;20}21if (this._howMany === 0) {22this._resolve([]);23return;24}25this._init$(undefined, -5);26var isArrayResolved = isArray(this._values);27if (!this._isResolved() &&28isArrayResolved &&29this._howMany > this._canPossiblyFulfill()) {30this._reject(this._getRangeError(this.length()));31}32};3334SomePromiseArray.prototype.init = function () {35this._initialized = true;36this._init();37};3839SomePromiseArray.prototype.setUnwrap = function () {40this._unwrap = true;41};4243SomePromiseArray.prototype.howMany = function () {44return this._howMany;45};4647SomePromiseArray.prototype.setHowMany = function (count) {48this._howMany = count;49};5051SomePromiseArray.prototype._promiseFulfilled = function (value) {52this._addFulfilled(value);53if (this._fulfilled() === this.howMany()) {54this._values.length = this.howMany();55if (this.howMany() === 1 && this._unwrap) {56this._resolve(this._values[0]);57} else {58this._resolve(this._values);59}60}6162};63SomePromiseArray.prototype._promiseRejected = function (reason) {64this._addRejected(reason);65if (this.howMany() > this._canPossiblyFulfill()) {66var e = new AggregateError();67for (var i = this.length(); i < this._values.length; ++i) {68e.push(this._values[i]);69}70this._reject(e);71}72};7374SomePromiseArray.prototype._fulfilled = function () {75return this._totalResolved;76};7778SomePromiseArray.prototype._rejected = function () {79return this._values.length - this.length();80};8182SomePromiseArray.prototype._addRejected = function (reason) {83this._values.push(reason);84};8586SomePromiseArray.prototype._addFulfilled = function (value) {87this._values[this._totalResolved++] = value;88};8990SomePromiseArray.prototype._canPossiblyFulfill = function () {91return this.length() - this._rejected();92};9394SomePromiseArray.prototype._getRangeError = function (count) {95var message = "Input array must contain at least " +96this._howMany + " items but contains only " + count + " items";97return new RangeError(message);98};99100SomePromiseArray.prototype._resolveEmptyArray = function () {101this._reject(this._getRangeError(0));102};103104function some(promises, howMany) {105if ((howMany | 0) !== howMany || howMany < 0) {106return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/1wAmHx\u000a");107}108var ret = new SomePromiseArray(promises);109var promise = ret.promise();110ret.setHowMany(howMany);111ret.init();112return promise;113}114115Promise.some = function (promises, howMany) {116return some(promises, howMany);117};118119Promise.prototype.some = function (howMany) {120return some(this, howMany);121};122123Promise._SomePromiseArray = SomePromiseArray;124};125126127