Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
module.exports =
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;
8
9
10
function SomePromiseArray(values) {
11
this.constructor$(values);
12
this._howMany = 0;
13
this._unwrap = false;
14
this._initialized = false;
15
}
16
util.inherits(SomePromiseArray, PromiseArray);
17
18
SomePromiseArray.prototype._init = function () {
19
if (!this._initialized) {
20
return;
21
}
22
if (this._howMany === 0) {
23
this._resolve([]);
24
return;
25
}
26
this._init$(undefined, -5);
27
var isArrayResolved = isArray(this._values);
28
if (!this._isResolved() &&
29
isArrayResolved &&
30
this._howMany > this._canPossiblyFulfill()) {
31
this._reject(this._getRangeError(this.length()));
32
}
33
};
34
35
SomePromiseArray.prototype.init = function () {
36
this._initialized = true;
37
this._init();
38
};
39
40
SomePromiseArray.prototype.setUnwrap = function () {
41
this._unwrap = true;
42
};
43
44
SomePromiseArray.prototype.howMany = function () {
45
return this._howMany;
46
};
47
48
SomePromiseArray.prototype.setHowMany = function (count) {
49
this._howMany = count;
50
};
51
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]);
58
} else {
59
this._resolve(this._values);
60
}
61
}
62
63
};
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]);
70
}
71
this._reject(e);
72
}
73
};
74
75
SomePromiseArray.prototype._fulfilled = function () {
76
return this._totalResolved;
77
};
78
79
SomePromiseArray.prototype._rejected = function () {
80
return this._values.length - this.length();
81
};
82
83
SomePromiseArray.prototype._addRejected = function (reason) {
84
this._values.push(reason);
85
};
86
87
SomePromiseArray.prototype._addFulfilled = function (value) {
88
this._values[this._totalResolved++] = value;
89
};
90
91
SomePromiseArray.prototype._canPossiblyFulfill = function () {
92
return this.length() - this._rejected();
93
};
94
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);
99
};
100
101
SomePromiseArray.prototype._resolveEmptyArray = function () {
102
this._reject(this._getRangeError(0));
103
};
104
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");
108
}
109
var ret = new SomePromiseArray(promises);
110
var promise = ret.promise();
111
ret.setHowMany(howMany);
112
ret.init();
113
return promise;
114
}
115
116
Promise.some = function (promises, howMany) {
117
return some(promises, howMany);
118
};
119
120
Promise.prototype.some = function (howMany) {
121
return some(this, howMany);
122
};
123
124
Promise._SomePromiseArray = SomePromiseArray;
125
};
126
127