-
Notifications
You must be signed in to change notification settings - Fork 42
/
Promise.js
271 lines (217 loc) · 7.21 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
/*
* @Author: dm.yang
* @Date: 2014-11-28 12:55:44
* @Last Modified by: dmyang
* @Last Modified time: 2015-08-05 20:33:01
*/
// @see => http://promises-aplus.github.io/promises-spec/
// @see => https://github.com/cujojs/when/blob/master/lib/makePromise.js
;(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {// CommonJS
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {// AMD / RequireJS
define(factory);
} else {
root.Promise = factory.call(root);
}
}(this, function() {
'use strict';
function PromiseA(resolver) {
if(!(this instanceof PromiseA)) return new PromiseA(resolver);
this.status = 'pending';
this.value;
this.reason;
// then may be called multiple times on the same promise
this._resolves = [];
this._rejects = [];
if(isFn(resolver)) resolver(this.resolve.bind(this), this.reject.bind(this));
return this;
};
PromiseA.prototype.then = function(resolve, reject) {
var next = this._next || (this._next = PromiseA());
var status = this.status;
var x;
if('pending' === status) {
isFn(resolve) && this._resolves.push(resolve);
isFn(reject) && this._rejects.push(reject);
return next;
}
if('resolved' === status) {
if(!isFn(resolve)) {
next.resolve(resolve);
} else {
try {
x = resolve(this.value);
resolveX(next, x);
} catch(e) {
next.reject(e);
}
}
return next;
}
if('rejected' === status) {
if(!isFn(reject)) {
next.reject(reject);
} else {
try {
x = reject(this.reason);
resolveX(next, x);
} catch(e) {
next.reject(e);
}
}
return next;
}
};
PromiseA.prototype.resolve = function(value) {
if('rejected' === this.status) throw Error('Illegal call.');
this.status = 'resolved';
this.value = value;
this._resolves.length && fireQ(this);
return this;
};
PromiseA.prototype.reject = function(reason) {
if('resolved' === this.status) throw Error('Illegal call.');
this.status = 'rejected';
this.reason = reason;
this._rejects.length && fireQ(this);
return this;
};
// shortcut of promise.then(undefined, reject)
PromiseA.prototype.catch = function(reject) {
return this.then(void 0, reject);
};
// return a promise with another promise passing in
PromiseA.cast = function(arg) {
var p = PromiseA();
if(arg instanceof PromiseA) return resolvePromise(p, arg);
else return PromiseA.resolve(arg);
};
// return a promise which resolved with arg
// the arg maybe a thanable object or thanable function or other
PromiseA.resolve = function(arg) {
var p = PromiseA();
if(isThenable(arg)) return resolveThen(p, arg);
else return p.resolve(arg);
};
// accept a promises array,
// return a promise which will resolsed with all promises's value,
// if any promise passed rejectd, the returned promise will rejected with the same reason
PromiseA.all = function(promises) {
var len = promises.length;
var promise = PromiseA();
var r = [];
var pending = 0;
var locked;
each(promises, function(p, i) {
p.then(function(v) {
r[i] = v;
if(++pending === len && !locked) promise.resolve(r);
}, function(e) {
locked = true;
promise.reject(e);
});
});
return promise;
};
// accept a promises array,
// return a promise which will resolsed with the first resolved promise passed,
// if any promise passed rejectd, the returned promise will rejected with the same reason
PromiseA.any = function(promises) {
var promise = PromiseA();
var called;
each(promises, function(p, i) {
p.then(function(v) {
if(!called) {
promise.resolve(v);
called = true;
}
}, function(e) {
called = true;
promise.reject(e);
});
});
return promise;
};
// return a promise which reject with reason
// reason must be an instance of Error object
PromiseA.reject = function(reason) {
if(!(reason instanceof Error)) throw Error('reason must be an instance of Error');
var p = PromiseA();
p.reject(reason);
return p;
};
function resolveX(promise, x) {
if(x === promise) promise.reject(new Error('TypeError'));
if(x instanceof Promise) return resolvePromise(promise, x);
else if(isThenable(x)) return resolveThen(promise, x);
else return promise.resolve(x);
};
function resolvePromise(promise1, promise2) {
var status = promise2.status;
if('pending' === status) return promise2.then(promise1.resolve.bind(promise1), promise1.reject.bind(promise1));
if('resolved' === status) return promise1.resolve(promise2.value);
if('rejected' === status) return promise1.reject(promise2.reason);
};
function resolveThen(promise, thanable) {
var called;
var resolve = once(function(x) {
if(called) return;
resolveX(promise, x);
called = true;
});
var reject = once(function(r) {
if(called) return;
promise.reject(r);
called = true;
});
try {
thanable.then.call(thanable, resolve, reject);
} catch(e) {
if(!called) throw e;
else promise.reject(e);
}
return promise;
};
function fireQ(promise) {
var status = promise.status;
var queue = promise['resolved' === status ? '_resolves' : '_rejects'];
var arg = promise['resolved' === status ? 'value' : 'reason'];
var fn;
var x;
while(fn = queue.shift()) {
x = fn.call(promise, arg);
x && resolveX(promise._next, x);
}
return promise;
};
function noop () {};
function isFn(fn) {
return 'function' === type(fn);
};
function isObj(o) {
return 'object' === type(o);
};
function type(obj) {
var o = {};
return o.toString.call(obj).replace(/\[object (\w+)\]/, '$1').toLowerCase();
};
function isThenable(obj) {
return obj && obj.then && isFn(obj.then);
};
function once(fn) {
var called;
var r;
return function() {
if(called) return r;
called = true;
return r = fn.apply(this, arguments);
};
};
// maybe faster then `forEach()`
function each(arr, iterator) {
var i = 0;
for(; arr[i++]; ) iterator(arr[i], i, arr);
};
return PromiseA;
}));