es6-extensions.js
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
//This file contains the ES6 extensions to the core Promises/A+ API
var Promise = require('./core.js');
module.exports = Promise;
/* Static Functions */
var TRUE = valuePromise(true);
var FALSE = valuePromise(false);
var NULL = valuePromise(null);
var UNDEFINED = valuePromise(undefined);
var ZERO = valuePromise(0);
var EMPTYSTRING = valuePromise('');
function valuePromise(value) {
var p = new Promise(Promise._D);
p._y = 1;
p._z = value;
return p;
}
Promise.resolve = function (value) {
if (value instanceof Promise) return value;
if (value === null) return NULL;
if (value === undefined) return UNDEFINED;
if (value === true) return TRUE;
if (value === false) return FALSE;
if (value === 0) return ZERO;
if (value === '') return EMPTYSTRING;
if (typeof value === 'object' || typeof value === 'function') {
try {
var then = value.then;
if (typeof then === 'function') {
return new Promise(then.bind(value));
}
} catch (ex) {
return new Promise(function (resolve, reject) {
reject(ex);
});
}
}
return valuePromise(value);
};
var iterableToArray = function (iterable) {
if (typeof Array.from === 'function') {
// ES2015+, iterables exist
iterableToArray = Array.from;
return Array.from(iterable);
}
// ES5, only arrays and array-likes exist
iterableToArray = function (x) { return Array.prototype.slice.call(x); };
return Array.prototype.slice.call(iterable);
}
Promise.all = function (arr) {
var args = iterableToArray(arr);
return new Promise(function (resolve, reject) {
if (args.length === 0) return resolve([]);
var remaining = args.length;
function res(i, val) {
if (val && (typeof val === 'object' || typeof val === 'function')) {
if (val instanceof Promise && val.then === Promise.prototype.then) {
while (val._y === 3) {
val = val._z;
}
if (val._y === 1) return res(i, val._z);
if (val._y === 2) reject(val._z);
val.then(function (val) {
res(i, val);
}, reject);
return;
} else {
var then = val.then;
if (typeof then === 'function') {
var p = new Promise(then.bind(val));
p.then(function (val) {
res(i, val);
}, reject);
return;
}
}
}
args[i] = val;
if (--remaining === 0) {
resolve(args);
}
}
for (var i = 0; i < args.length; i++) {
res(i, args[i]);
}
});
};
function onSettledFulfill(value) {
return { status: 'fulfilled', value: value };
}
function onSettledReject(reason) {
return { status: 'rejected', reason: reason };
}
function mapAllSettled(item) {
if(item && (typeof item === 'object' || typeof item === 'function')){
if(item instanceof Promise && item.then === Promise.prototype.then){
return item.then(onSettledFulfill, onSettledReject);
}
var then = item.then;
if (typeof then === 'function') {
return new Promise(then.bind(item)).then(onSettledFulfill, onSettledReject)
}
}
return onSettledFulfill(item);
}
Promise.allSettled = function (iterable) {
return Promise.all(iterableToArray(iterable).map(mapAllSettled));
};
Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
reject(value);
});
};
Promise.race = function (values) {
return new Promise(function (resolve, reject) {
iterableToArray(values).forEach(function(value){
Promise.resolve(value).then(resolve, reject);
});
});
};
/* Prototype Methods */
Promise.prototype['catch'] = function (onRejected) {
return this.then(null, onRejected);
};
function getAggregateError(errors){
if(typeof AggregateError === 'function'){
return new AggregateError(errors,'All promises were rejected');
}
var error = new Error('All promises were rejected');
error.name = 'AggregateError';
error.errors = errors;
return error;
}
Promise.any = function promiseAny(values) {
return new Promise(function(resolve, reject) {
var promises = iterableToArray(values);
var hasResolved = false;
var rejectionReasons = [];
function resolveOnce(value) {
if (!hasResolved) {
hasResolved = true;
resolve(value);
}
}
function rejectionCheck(reason) {
rejectionReasons.push(reason);
if (rejectionReasons.length === promises.length) {
reject(getAggregateError(rejectionReasons));
}
}
if(promises.length === 0){
reject(getAggregateError(rejectionReasons));
} else {
promises.forEach(function(value){
Promise.resolve(value).then(resolveOnce, rejectionCheck);
});
}
});
};