-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
288 lines (265 loc) · 7.89 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
'use strict';
var PENDING = 0;
var FULFILLED = 1;
var REJECTED = 2;
var nextTick = setTimeout;
if (typeof setImmediate === 'function') {
nextTick = setImmediate;
} else if (typeof process === 'object' && typeof process.nextTick === 'function') {
nextTick = process.nextTick;
}
function Promise(executor) {
var state = PENDING;
// store value once FULFILLED or REJECTED
var value = null;
// store sucess & failure handlers
var handlers = [];
function fulfill(result) {
state = FULFILLED;
value = result;
handlers.forEach(handle);
handlers = null;
}
function reject(error) {
state = REJECTED;
value = error;
handlers.forEach(handle);
handlers = null;
}
function resolve(result) {
try {
var then = getThen(result);
if (then) {
doResolve(function() {
then.apply(result, arguments);
}, resolve, reject);
return;
}
fulfill(result);
} catch (e) {
reject(e);
}
}
function handle(handler) {
if (state === PENDING) {
handlers.push(handler);
} else {
if (state === FULFILLED &&
typeof handler.onFulfilled === 'function') {
handler.onFulfilled(value);
}
if (state === REJECTED &&
typeof handler.onRejected === 'function') {
handler.onRejected(value);
}
}
}
this.done = function(onFulfilled, onRejected) {
// ensure we are always asynchronous
nextTick(function() {
handle({
onFulfilled: onFulfilled,
onRejected: onRejected
});
}, 0);
};
var then = function(onFulfilled, onRejected) {
var self = this;
var res;
var npromise = new Promise(function(resolve, reject) {
return self.done(function(result) {
if (typeof onFulfilled === 'function') {
try {
res = onFulfilled(result);
if (res === npromise) {
return reject(new TypeError('The `promise` and `x` refer to the same object.'));
}
return resolve(res);
} catch (e) {
return reject(e);
}
} else {
return resolve(result);
}
}, function(error) {
if (typeof onRejected === 'function') {
try {
res = onRejected(error);
if (res === npromise) {
return reject(new TypeError('The `promise` and `x` refer to the same object.'));
}
return resolve(res);
} catch (ex) {
return reject(ex);
}
} else {
return reject(error);
}
});
});
return npromise;
};
this.then = then;
this.catch = function(onRejected) {
return then.call(this, undefined, onRejected);
};
doResolve(executor, resolve, reject);
}
/**
* Check if a value is a Promise and, if it is,
* return the `then` method of that promise.
*
* @param {Promise|Any} value
* @return {Function|Null}
*/
function getThen(value) {
var type = typeof value;
var then;
if (value && (type === 'object' || type === 'function')) {
then = value.then;
if (typeof then === 'function') {
return then;
}
}
return null;
}
/**
* Take a potentially misbehaving resolver function and make sure
* onFulfilled and onRejected are only called once.
*
* ensure asynchronous execution of onFulfilled/onRejected
*
* @param {Function} fn A resolver function that may not be trusted
* @param {Function} onFulfilled
* @param {Function} onRejected
*/
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function(value) {
if (done) {
return;
}
done = true;
nextTick(function() {
onFulfilled(value);
}, 0);
}, function(reason) {
if (done) {
return;
}
done = true;
nextTick(function() {
onRejected(reason);
}, 0);
});
} catch (e) {
if (done) {
return;
}
done = true;
nextTick(function() {
onRejected(e);
}, 0);
}
}
Promise.resolve = function(value) {
return new Promise(function(resolve, reject) {
resolve(value);
});
};
Promise.reject = function(reason) {
return new Promise(function(resolve, reject) {
reject(reason);
});
};
/**
* Promise.all
* returns a promise that resolves when all of the promises in the iterable argument
* have resolved, or rejects with the reason of the first passed promise that rejects.
*
* support Promise.all(''), Promise.all(promises) and so on.
*
* @param {Iterable} iterable iterable object, like Array, String
* @return {Promise} promise
*/
Promise.all = function(iterable) {
var results, len, promise, i;
var resolved = 0;
if (iterable == null || typeof iterable.length !== 'number') {
throw new Error('ArgumentsError: argument should be iterable.');
}
len = iterable.length;
return len === 0 ? Promise.resolve([]) : new Promise(function(resolve, reject) {
results = new Array(len);
for (i = 0; i < len; i++) {
(function(i) {
promise = iterable[i];
if (!(promise instanceof Promise)) {
promise = Promise.resolve(promise);
}
promise.catch(function(reason) {
reject(reason);
});
promise.then(function(value) {
results[i] = value;
if (++resolved === len) {
resolve(results);
}
});
})(i);
}
});
};
/**
* Promise.race
* Returns a promise that resolves or rejects as soon as one of the promises
* in the iterable resolves or rejects,
* with the value or reason from that promise.
*
* @param {Iterable} iterable iterable object, like Array, String
* @return {Promise} promise
*/
Promise.race = function(iterable) {
var deferred, len, i, promise;
if (iterable == null || typeof iterable.length !== 'number') {
throw new Error('ArgumentsError: argument should be iterable.');
}
deferred = Promise.deferred();
len = iterable.length;
for (i = 0; i < len; i++) {
promise = iterable[i];
if (promise instanceof Promise) {
promise.then(function(value) {
deferred.resolve(value);
}, function(reason) {
deferred.reject(reason);
});
} else {
// if not promise, immediately resolve result promise
deferred.resolve(promise);
break;
}
}
return deferred.promise;
};
/**
* Promise.deferred
* return the `deferred` object binding to a promise.
* And you can control the promise via `deferred.resolve`/`deferred.reject`.
*
* @param {Promise|Any} value
* @return {Function|Null}
*/
Promise.deferred = function() {
var deferred = {};
deferred.promise = new Promise(function(resolve,reject){
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
};
// Official API (especially chrome) is Promise.defer
// so just add Promise.defer and let it refer to Promise.deferred
Promise.defer = Promise.deferred;
module.exports = Promise;