3 function(Promise, PromiseArray, apiRejection) {
4 var util = require("./util.js");
5 var RangeError = require("./errors.js").RangeError;
6 var AggregateError = require("./errors.js").AggregateError;
7 var isArray = util.isArray;
10 function SomePromiseArray(values) {
11 this.constructor$(values);
14 this._initialized = false;
16 util.inherits(SomePromiseArray, PromiseArray);
18 SomePromiseArray.prototype._init = function () {
19 if (!this._initialized) {
22 if (this._howMany === 0) {
26 this._init$(undefined, -5);
27 var isArrayResolved = isArray(this._values);
28 if (!this._isResolved() &&
30 this._howMany > this._canPossiblyFulfill()) {
31 this._reject(this._getRangeError(this.length()));
35 SomePromiseArray.prototype.init = function () {
36 this._initialized = true;
40 SomePromiseArray.prototype.setUnwrap = function () {
44 SomePromiseArray.prototype.howMany = function () {
48 SomePromiseArray.prototype.setHowMany = function (count) {
49 this._howMany = count;
52 SomePromiseArray.prototype._promiseFulfilled = function (value) {
53 this._addFulfilled(value);
54 if (this._fulfilled() === this.howMany()) {
55 this._values.length = this.howMany();
56 if (this.howMany() === 1 && this._unwrap) {
57 this._resolve(this._values[0]);
59 this._resolve(this._values);
64 SomePromiseArray.prototype._promiseRejected = function (reason) {
65 this._addRejected(reason);
66 if (this.howMany() > this._canPossiblyFulfill()) {
67 var e = new AggregateError();
68 for (var i = this.length(); i < this._values.length; ++i) {
69 e.push(this._values[i]);
75 SomePromiseArray.prototype._fulfilled = function () {
76 return this._totalResolved;
79 SomePromiseArray.prototype._rejected = function () {
80 return this._values.length - this.length();
83 SomePromiseArray.prototype._addRejected = function (reason) {
84 this._values.push(reason);
87 SomePromiseArray.prototype._addFulfilled = function (value) {
88 this._values[this._totalResolved++] = value;
91 SomePromiseArray.prototype._canPossiblyFulfill = function () {
92 return this.length() - this._rejected();
95 SomePromiseArray.prototype._getRangeError = function (count) {
96 var message = "Input array must contain at least " +
97 this._howMany + " items but contains only " + count + " items";
98 return new RangeError(message);
101 SomePromiseArray.prototype._resolveEmptyArray = function () {
102 this._reject(this._getRangeError(0));
105 function some(promises, howMany) {
106 if ((howMany | 0) !== howMany || howMany < 0) {
107 return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/1wAmHx\u000a");
109 var ret = new SomePromiseArray(promises);
110 var promise = ret.promise();
111 ret.setHowMany(howMany);
116 Promise.some = function (promises, howMany) {
117 return some(promises, howMany);
120 Promise.prototype.some = function (howMany) {
121 return some(this, howMany);
124 Promise._SomePromiseArray = SomePromiseArray;