-
Notifications
You must be signed in to change notification settings - Fork 1
/
PromisePolyfill.js
157 lines (145 loc) · 3.33 KB
/
PromisePolyfill.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
const STATE = {
FULFILLED: "fulfilled",
PENDING: "pending",
REJECTED: "rejected",
};
class MyPromise {
#onSuccessBind = this.#onSuccess.bind(this);
#onFailureBind = this.#onFailure.bind(this);
#state = STATE.PENDING;
#value = null;
#thenCbs = [];
#catchCbs = [];
constructor(callback) {
try {
callback(this.#onSuccessBind, this.#onFailureBind);
} catch (e) {
this.#onFailure(e);
}
}
#onSuccess(value) {
if (this.#state !== STATE.PENDING) return;
queueMicrotask(() => {
/**
* In case the callback or promise depends on resolving
* another promise
*/
if (value instanceof MyPromise) {
value.then(this.#onSuccessBind, this.#onFailureBind);
return;
}
this.#value = value;
this.#state = STATE.FULFILLED;
this.#runCallbacks();
});
}
#onFailure(value) {
if (this.#state !== STATE.PENDING) return;
queueMicrotask(() => {
/**
* In case the value is a promise. we want to first
* want this value to resolve then we want to run the
* next promise
*/
if (value instanceof MyPromise) {
value.then(this.#onSuccessBind, this.#onFailureBind);
return;
}
if (this.#catchCbs.length === 0) {
throw new UncaughtPromiseError(value);
}
this.#value = value;
this.#state = STATE.REJECTED;
this.#runCallbacks();
});
}
#runCallbacks() {
if (this.#state === STATE.FULFILLED) {
this.#thenCbs.forEach((callback) => {
callback(this.#value);
});
this.#thenCbs = [];
}
if (this.#state === STATE.REJECTED) {
this.#catchCbs.forEach((callback) => {
callback(this.#value);
});
this.#catchCbs = [];
}
}
then(thenCb, catchCb) {
return new Promise((resolve, reject) => {
this.#thenCbs.push((result) => {
console.log("RESULT", result);
if (thenCb === null) {
resolve(result);
return;
}
try {
let res = thenCb(result);
resolve(res);
} catch (error) {
reject(error);
}
});
this.#catchCbs.push((result) => {
console.log("RESULT", result);
if (catchCb === null) {
reject(result);
return;
}
try {
let res = catchCb(result);
resolve(res);
} catch (error) {
reject(error);
}
});
this.#runCallbacks();
});
}
catch(cb) {
return this.then(undefined, cb);
}
finally(cb) {
return this.then(
(result) => {
cb();
return result;
},
(result) => {
cb();
return result;
}
);
}
static resolve(value) {
return new MyPromise((resolve) => {
resolve(value);
});
}
static reject(value) {
return new MyPromise((resolve, reject) => {
reject(value);
});
}
}
class UncaughtPromiseError extends Error {
constructor(error) {
super(error);
this.stack = `(in promise) ${error.stack}`;
}
}
let pro = new MyPromise((resolve, reject) => {
resolve(200);
});
pro
.then((res) => {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
reject(res + 40);
}, 200);
});
})
.then((r) => console.log("Hey", r))
.catch((err) => console.error(err));