# 前言
话不多说,直接上代码
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
const runMicroTask = callback => {
Promise.resolve().then(callback);
}
const isPromise = data => {
return typeof data === 'object' && typeof data.then === 'function'
}
class MyPromise {
constructor(executor) {
this._value = undefined;
this._state = PENDING;
this._handlers = [];
try {
executor(this._resolve.bind(this), this._reject.bind(this));
} catch (error) {
this._reject(error);
}
}
_changeState(value, state) {
if (this._state !== PENDING) {
return;
}
if(isPromise(value)) {
value.then(val => this._resolve(val),reason => this._reject(reason));
return;
}
this._value = value;
this._state = state;
this._runHandlers();
}
_resolve(val) {
this._changeState(val, FULFILLED);
}
_reject(reason) {
this._changeState(reason, REJECTED);
}
then(onFulfilled, onRejected) {
return new MyPromise((resolve, reject) => {
this._handlersPush(onFulfilled, FULFILLED, resolve, reject);
this._handlersPush(onRejected, REJECTED, resolve, reject);
this._runHandlers();
})
}
_handlersPush(executor, state, resolve, reject) {
this._handlers.push({ executor, state, resolve, reject });
}
_runHandlers() {
if (this._state === PENDING) {
return;
}
while (this._handlers[0]) {
const handler = this._handlers.shift();
this._runHandler(handler);
}
}
_runHandler({ executor, state, resolve, reject }) {
runMicroTask(() => {
if (state !== this._state) {
return;
}
if (typeof executor !== 'function') {
this._state === FULFILLED ? resolve(this._value) : reject(this._value);
return;
}
try {
const result = executor(this._value);
if (isPromise(result)) {
result.then(resolve, reject);
return;
}
resolve(result)
} catch (error) {
reject(error)
}
})
}
catch(onRejected) {
return this.then(null, onRejected);
}
finally(onSettled) {
return this.then(
val => {
onSettled();
return val;
},
reason => {
onSettled();
return reason;
})
}
static resolve(val) {
return new MyPromise(resolve => resolve(val));
}
static reject(reason) {
return new MyPromise((_, reject) => reject(reason))
}
static all(ps) {
return new MyPromise((resolve, reject) => {
const result = [];
const leg = ps.length;
let count = 0;
for (let i=0; i< leg; i++) {
MyPromise.resolve(ps[i]).then(val => {
count++;
result[i] = val;
if (count === leg) {
resolve(result)
}
}, reject)
}
if (leg === 0) {
resolve(result);
}
})
}
static allSettled(promises) {
const result = [];
for (let promise of promises) {
result.push(MyPromise.resolve(promise).then(val => {
return {
state: FULFILLED,
val,
}
}).catch(reason => {
return {
state: REJECTED,
reason,
}
})
)
}
return MyPromise.all(result);
}
static race(promises) {
return new MyPromise((resolve, reject) => {
for (let promise of promises) {
MyPromise.resolve(promise).then(resolve).catch(reject)
}
})
}
}
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
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