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
let Promise = window.Promise;
if (!Promise) {
Promise = function (executor) {
executor(this.resolve.bind(this), this.reject.bind(this));
this._thens = [];
};
Promise.prototype = {
then: function (onResolve, onReject, onProgress) {
this._thens.push({resolve: onResolve, reject: onReject, progress: onProgress});
},
'catch': function (onReject) {
this._thens.push({reject: onReject});
},
resolve: function (value) {
this._complete('resolve', value);
},
reject: function (reason) {
this._complete('reject', reason);
},
progress: function (status) {
let i = 0;
let aThen;
while (aThen = this._thens[i++]) {
aThen.progress && aThen.progress(status);
}
},
_complete: function (which, arg) {
this.then = which === 'resolve' ?
function (resolve) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
let aThen;
let i = 0;
while (aThen = this._thens[i++]) {
aThen[which] && aThen[which](arg);
}
delete this._thens;
}
};
}
export default Promise;