This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
promise.js
310 lines (284 loc) · 9.18 KB
/
promise.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* Promise polyfill v1.0.10
* requires setImmediate
*
* © 2014–2015 Dmitry Korobkin
* Released under the MIT license
* github.com/Octane/Promise
*/
(function (global) {'use strict';
var STATUS = '[[PromiseStatus]]';
var VALUE = '[[PromiseValue]]';
var ON_FUlFILLED = '[[OnFulfilled]]';
var ON_REJECTED = '[[OnRejected]]';
var ORIGINAL_ERROR = '[[OriginalError]]';
var PENDING = 'pending';
var INTERNAL_PENDING = 'internal pending';
var FULFILLED = 'fulfilled';
var REJECTED = 'rejected';
var NOT_ARRAY = 'not an array.';
var REQUIRES_NEW = 'constructor Promise requires "new".';
var CHAINING_CYCLE = 'then() cannot return same Promise that it resolves.';
var setImmediate = global.setImmediate || require('timers').setImmediate;
var isArray = Array.isArray || function (anything) {
return Object.prototype.toString.call(anything) == '[object Array]';
};
function InternalError(originalError) {
this[ORIGINAL_ERROR] = originalError;
}
function isInternalError(anything) {
return anything instanceof InternalError;
}
function isObject(anything) {
//Object.create(null) instanceof Object → false
return Object(anything) === anything;
}
function isCallable(anything) {
return typeof anything == 'function';
}
function isPromise(anything) {
return anything instanceof Promise;
}
function identity(value) {
return value;
}
function thrower(reason) {
throw reason;
}
function enqueue(promise, onFulfilled, onRejected) {
if (!promise[ON_FUlFILLED]) {
promise[ON_FUlFILLED] = [];
promise[ON_REJECTED] = [];
}
promise[ON_FUlFILLED].push(onFulfilled);
promise[ON_REJECTED].push(onRejected);
}
function clearAllQueues(promise) {
delete promise[ON_FUlFILLED];
delete promise[ON_REJECTED];
}
function callEach(queue) {
var i;
var length = queue.length;
for (i = 0; i < length; i++) {
queue[i]();
}
}
function call(resolve, reject, value) {
var anything = toPromise(value);
if (isPromise(anything)) {
anything.then(resolve, reject);
} else if (isInternalError(anything)) {
reject(anything[ORIGINAL_ERROR]);
} else {
resolve(value);
}
}
function toPromise(anything) {
var then;
if (isPromise(anything)) {
return anything;
}
if(isObject(anything)) {
try {
then = anything.then;
} catch (error) {
return new InternalError(error);
}
if (isCallable(then)) {
return new Promise(function (resolve, reject) {
setImmediate(function () {
try {
then.call(anything, resolve, reject);
} catch (error) {
reject(error);
}
});
});
}
}
return null;
}
function resolvePromise(promise, resolver) {
function resolve(value) {
if (promise[STATUS] == PENDING) {
fulfillPromise(promise, value);
}
}
function reject(reason) {
if (promise[STATUS] == PENDING) {
rejectPromise(promise, reason);
}
}
try {
resolver(resolve, reject);
} catch(error) {
reject(error);
}
}
function fulfillPromise(promise, value) {
var queue;
var anything = toPromise(value);
if (isPromise(anything)) {
promise[STATUS] = INTERNAL_PENDING;
anything.then(
function (value) {
fulfillPromise(promise, value);
},
function (reason) {
rejectPromise(promise, reason);
}
);
} else if (isInternalError(anything)) {
rejectPromise(promise, anything[ORIGINAL_ERROR]);
} else {
promise[STATUS] = FULFILLED;
promise[VALUE] = value;
queue = promise[ON_FUlFILLED];
if (queue && queue.length) {
clearAllQueues(promise);
callEach(queue);
}
}
}
function rejectPromise(promise, reason) {
var queue = promise[ON_REJECTED];
promise[STATUS] = REJECTED;
promise[VALUE] = reason;
if (queue && queue.length) {
clearAllQueues(promise);
callEach(queue);
}
}
function Promise(resolver) {
var promise = this;
if (!isPromise(promise)) {
throw new TypeError(REQUIRES_NEW);
}
promise[STATUS] = PENDING;
promise[VALUE] = undefined;
resolvePromise(promise, resolver);
}
Promise.prototype.then = function (onFulfilled, onRejected) {
var promise = this;
var nextPromise;
onFulfilled = isCallable(onFulfilled) ? onFulfilled : identity;
onRejected = isCallable(onRejected) ? onRejected : thrower;
nextPromise = new Promise(function (resolve, reject) {
function tryCall(func) {
var value;
try {
value = func(promise[VALUE]);
} catch (error) {
reject(error);
return;
}
if (value === nextPromise) {
reject(new TypeError(CHAINING_CYCLE));
} else {
call(resolve, reject, value);
}
}
function asyncOnFulfilled() {
setImmediate(tryCall, onFulfilled);
}
function asyncOnRejected() {
setImmediate(tryCall, onRejected);
}
switch (promise[STATUS]) {
case FULFILLED:
asyncOnFulfilled();
break;
case REJECTED:
asyncOnRejected();
break;
default:
enqueue(promise, asyncOnFulfilled, asyncOnRejected);
}
});
return nextPromise;
};
Promise.prototype['catch'] = function (onRejected) {
return this.then(identity, onRejected);
};
Promise.resolve = function (value) {
var anything = toPromise(value);
if (isPromise(anything)) {
return anything;
}
return new Promise(function (resolve, reject) {
if (isInternalError(anything)) {
reject(anything[ORIGINAL_ERROR]);
} else {
resolve(value);
}
});
};
Promise.reject = function (reason) {
return new Promise(function (resolve, reject) {
reject(reason);
});
};
Promise.race = function (values) {
return new Promise(function (resolve, reject) {
var i;
var length;
if (isArray(values)) {
length = values.length;
for (i = 0; i < length; i++) {
call(resolve, reject, values[i]);
}
} else {
reject(new TypeError(NOT_ARRAY));
}
});
};
Promise.all = function (values) {
return new Promise(function (resolve, reject) {
var fulfilledCount = 0;
var promiseCount = 0;
var anything;
var length;
var value;
var i;
if (isArray(values)) {
values = values.slice(0);
length = values.length;
for (i = 0; i < length; i++) {
value = values[i];
anything = toPromise(value);
if (isPromise(anything)) {
promiseCount++;
anything.then(
function (index) {
return function (value) {
values[index] = value;
fulfilledCount++;
if (fulfilledCount == promiseCount) {
resolve(values);
}
};
}(i),
reject
);
} else if (isInternalError(anything)) {
reject(anything[ORIGINAL_ERROR]);
} else {
//[1, , 3] → [1, undefined, 3]
values[i] = value;
}
}
if (!promiseCount) {
resolve(values);
}
} else {
reject(new TypeError(NOT_ARRAY));
}
});
};
if (typeof module != 'undefined' && module.exports) {
module.exports = global.Promise || Promise;
} else if (!global.Promise) {
global.Promise = Promise;
}
}(this));