-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElephant.js
198 lines (190 loc) · 4.64 KB
/
Elephant.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
// Refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
class Elephant {
constructor(fn) {
this.value = null;
this.state = 'pending';
this.isFinished = false;
this.resolverFn = null;
this.rejecterFn = null;
this.finallyFn = null;
try {
fn(this._resolve.bind(this), this._reject.bind(this));
} catch(e) {
return Elephant.reject(e);
}
}
static reject(err) {
return new Elephant((resolve, reject) => {
reject(err);
});
}
static resolve(val) {
return new Elephant((resolve, reject) => {
resolve(val);
});
}
static all(iter) {
const len = iter.length;
const resultArr = [];
let returnedCount = 0;
return new Elephant((resolve, reject) => {
iter.forEach((item, i) => {
item
.then(res => {
returnedCount++;
resultArr[i] = res;
if (returnedCount === len) {
resolve(resultArr);
}
})
.catch(res => reject(res))
});
});
}
static race(iter) {
return new Elephant((resolve, reject) => {
iter.forEach((item) => {
item
.then(res => resolve(res))
.catch(res => reject(res))
});
});
}
_resolve(val) {
this.isFinished = true;
this.value = val;
this.state = 'fulfilled';
if (this.resolverFn) {
this.resolverFn(val);
}
if (this.finallyFn) {
this.finallyFn(val);
}
}
_reject(val) {
this.isFinished = true;
this.value = val;
this.state = 'rejected';
setTimeout(() => {
if (this.rejecterFn) {
this.rejecterFn(val);
}
if (this.finallyFn) {
this.finallyFn(val);
}
}, 0)
}
_checkImmediateSettlement(state, handlerFn) {
if (this.isFinished && this.state === state) {
try {
const res = handlerFn(this.value);
if (res instanceof Elephant) {
return res;
}
return Elephant.resolve(res);
} catch(e) {
return Elephant.reject(e);
}
}
}
then(thenFn, catchFn) {
if (!thenFn) {
return new Elephant((resolve, reject) => {
this.resolverFn = (val) => resolve(val);
this.rejecterFn = (err) => reject(err);
});
}
const earlySettle = this._checkImmediateSettlement('fulfilled', thenFn);
if (earlySettle) {
return earlySettle;
}
if (catchFn) {
const earlyReject = this._checkImmediateSettlement('rejected', catchFn);
if (earlyReject) {
return earlyReject;
}
}
return new Elephant((resolve, reject) => {
if (!this.rejecterFn) {
this.rejecterFn = (error) => {
if (catchFn) {
try {
const res = catchFn(error);
if (res instanceof Elephant) {
return res
.then(val => resolve(val))
.catch(err => reject(err));
}
return resolve(res);
} catch(e) {
return reject(e);
}
}
return reject(error);
}
this._checkImmediateSettlement('rejected', this.rejecterFn);
}
this.resolverFn = (value) => {
try {
const res = thenFn(value);
if (res instanceof Elephant) {
return res
.then(val => resolve(val))
.catch(err => reject(err));
}
resolve(res);
} catch(e) {
reject(e);
}
}
});
}
catch(catchFn) {
const earlySettle = this._checkImmediateSettlement('rejected', catchFn);
if (earlySettle) {
return earlySettle;
}
return new Elephant((resolve, reject) => {
if (!this.resolverFn) {
this.resolverFn = (value) => {
resolve(value);
}
this._checkImmediateSettlement('fulfilled', this.resolverFn);
}
this.rejecterFn = (error) => {
try {
const res = catchFn(error);
if (res instanceof Elephant) {
return res
.then(val => resolve(val))
.catch(err => reject(err));
}
resolve(res);
} catch(e) {
reject(e);
}
}
});
}
finally(finallyFn) {
if (this.isFinished) {
try {
finallyFn();
return Elephant.resolve(this.value);
} catch(e) {
return Elephant.reject(e);
}
}
return new Elephant((resolve, reject) => {
this.finallyFn = (value) => {
try {
finallyFn();
resolve(value);
} catch(e) {
reject(e);
}
}
});
}
}
module.exports = Elephant;