-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyPromise.js
148 lines (128 loc) · 3.3 KB
/
myPromise.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
let Fromis9 = function Fromis9(callback) {
let _state = 'pending';
let _success;
let _error;
let _nextResolve;
let _nextReject;
let _resolve = function (value) {
_state = 'fulfilled';
queueMicrotask(() => {
try {
let result = _success && _success(value);
if (result instanceof Fromis9) {
result.then(
value => { _nextResolve && _nextResolve(value); },
error => { _nextReject && _nextReject(error); }
);
} else {
result = _success ? result : value;
_nextResolve && _nextResolve(result);
}
} catch (e) {
_nextReject && _nextReject(e);
}
});
}
let _reject = function (value) {
_state = 'rejected';
queueMicrotask(() => {
try {
let result = _error && _error(value);
if (result instanceof Fromis9) {
result.then(
value => { _nextResolve && _nextResolve(value); },
error => { _nextReject && _nextReject(error); }
);
} else {
if (_error) {
_nextResolve(result);
} else {
if (_nextReject) {
_nextReject(value);
} else {
console.error('Uncaught (in Fromis9)', value);
}
}
}
} catch (e) {
_nextReject && _nextReject(e);
}
});
};
this.then = function(success, error) {
_success = success;
_error = error;
return new Fromis9((resolve, reject) => {
_nextResolve = resolve;
_nextReject = reject;
});
}
this.catch = function(error) {
_error = error;
return new Fromis9((resolve, reject) => {
_nextResolve = resolve;
_nextReject = reject;
});
}
try {
callback(_resolve, _reject);
} catch (e) {
_reject(e);
}
};
Fromis9.resolve = function (value) {
return new Fromis9(resolve => resolve(value));
};
Fromis9.reject = function (value) {
return new Fromis9((resolve, reject) => reject(value));
};
Fromis9.all = function (fromis9s) {
let result = [];
let count = 0;
return new Fromis9((resolve, reject) => {
fromis9s.forEach((fromis9, index) => {
fromis9.then(
value => {
result[index] = value;
(++count == fromis9s.length) && resolve(result);
},
reject
)
});
});
};
Fromis9.race = function (fromis9s) {
let result = [];
let count = 0;
return new Fromis9((resolve, reject) => {
fromis9s.forEach((fromis9, index) => {
fromis9 = (fromis9 instanceof Fromis9) ? fromis9 : Fromis9.resolve(fromis9);
fromis9.then(
value => {
if (++count === 1) resolve(value);
},
error => {
if (++count === 1) reject(error);
},
);
});
});
};
Fromis9.allSettled = function (fromis9s) {
let result = [];
let count = 0;
return new Fromis9((resolve, reject) => {
fromis9s.forEach((fromis9, index) => {
fromis9.then(
value => {
result[index] = {status: 'fulfilled', value: value};
(++count == fromis9s.length) && resolve(result);
},
error => {
result[index] = {status: 'rejected', value: error};
(++count == fromis9s.length) && resolve(error);
},
);
});
});
};