-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.ts
207 lines (195 loc) · 7.37 KB
/
promise.ts
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
199
200
201
202
203
204
205
206
207
export enum States {
PENDING = "pending",
FULFILLED = "fulfilled",
REJECTED = "rejected",
}
export interface Resolve<T> {
(value: T | PromiseLike<T>): void;
}
export interface Reject {
(reason?: any): void;
}
export interface Executor<T> {
(resolve: Resolve<T>, reject: Reject): void;
}
// PromisesA+ 1.1
export interface PromiseLike<T> {
then<TResult1 = T, TResult2 = never>(
// PromisesA+ 2.2.1
onFulfilled?: ((value: T | PromiseLike<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): PromiseLike<TResult1 | TResult2>;
}
export class MyPromise<T> {
private state: States = States.PENDING;
private onFulfilledCbs = [] as (() => void)[]; // 成功时的回调函数
private onRejectedCbs = [] as (() => void)[]; // 失败时的回调函数
private value!: T | PromiseLike<T>;
private reason: any;
constructor(executor: Executor<T>) {
try {
executor(this.resolve, this.reject);
} catch (e) {
// PromisesA + 1.4
this.reject(e);
}
}
// PromisesA+ 1.3 | PromisesA+ 2.1
private resolve: Resolve<T> = (value: T | PromiseLike<T>) => {
try {
// PromisesA+ 2.2.4 异步调用,用setTimeout模拟创建微任务
setTimeout(() => {
if (this.state === States.PENDING) {
this.state = States.FULFILLED;
this.value = value;
// PromisesA+ 2.2.6.1
this.onFulfilledCbs.forEach((fn) => fn());
this.onFulfilledCbs = [];
}
});
} catch (e) {
this.reject(e);
}
};
// PromisesA+ 1.5
private reject: Reject = (reason: any) => {
try {
// PromisesA+ 2.2.4 异步调用,用setTimeout模拟创建微任务
setTimeout(() => {
if (this.state === States.PENDING) {
this.state = States.REJECTED;
this.reason = reason;
// PromisesA+ 2.2.6.2
this.onRejectedCbs.forEach((fn) => fn());
this.onRejectedCbs = [];
}
});
} catch (e) {
this.reject(e);
}
};
// PromisesA+ 1.2 | PromisesA+ 2.2
then<TResult1 = T, TResult2 = never>(
// PromisesA+ 2.2.1
onFulfilled?: ((value: T | PromiseLike<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): MyPromise<TResult1 | TResult2> {
// PromisesA+ 2.2.5 | PromisesA+ 2.2.7.3 | PromisesA+ 2.2.7.4
onFulfilled = typeof onFulfilled === "function" ? onFulfilled : (val: T | PromiseLike<T>) => val as any;
onRejected =
typeof onRejected === "function"
? onRejected
: (r: any) => {
throw r;
};
// PromisesA+ 2.2.7
const promise2 = new MyPromise<TResult1 | TResult2>((resolve: Resolve<TResult1 | TResult2>, reject: Reject) => {
if (this.state === States.FULFILLED) {
// PromisesA+ 2.2.2
// PromisesA+ 2.2.4 异步调用,用setTimeout模拟创建微任务
setTimeout(() => {
try {
// PromisesA+ 2.2.7.1
let x = onFulfilled!(this.value);
this.resolvePromise(promise2, x, resolve, reject);
} catch (e) {
// PromisesA+ 2.2.7.2
reject(e);
}
});
} else if (this.state === States.REJECTED) {
// PromisesA+ 2.2.3
// PromisesA+ 2.2.4 异步调用,用setTimeout模拟创建微任务
setTimeout(() => {
try {
// PromisesA+ 2.2.7.1
let x = onRejected!(this.reason);
this.resolvePromise(promise2, x, resolve, reject);
} catch (e) {
// PromisesA+ 2.2.7.2
reject(e);
}
});
} else if (this.state === States.PENDING) {
// PromisesA+ 2.2.6
// 调用回调函数时是异步的,因此这里不再需要加setTimeout
this.onFulfilledCbs.push(() => {
try {
let x = onFulfilled!(this.value);
// PromisesA+ 2.2.7.1
this.resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
this.onRejectedCbs.push(() => {
try {
let x = onRejected!(this.reason);
// PromisesA+ 2.2.7.1
this.resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
}
});
return promise2;
}
resolvePromise<T>(promise: MyPromise<T>, x: T | PromiseLike<T>, resolve: Resolve<T>, reject: Reject) {
// PromisesA+ 2.3.1
if (promise === x) {
const e = new TypeError("TypeError: Circular reference");
reject(e);
}
let called = false; // 防止resolve和reject多次调用
// PromisesA+ 2.3.3
if (x && (typeof x === "object" || typeof x === "function")) {
try {
// PromisesA+ 2.3.3.1 | PromisesA+ 2.3.2
const then = (x as PromiseLike<T>).then;
if (typeof then === "function") {
then.call(
x,
// PromisesA+ 2.3.3.3.1
(y: T | PromiseLike<T>) => {
// PromisesA+ 2.3.3.3.3
if (called) return;
called = true;
// 直到解析的对象不再是 thenable,取出其中的值
this.resolvePromise(promise, y, resolve, reject);
},
// PromisesA+ 2.3.3.3.2
(r: any) => {
// PromisesA+ 2.3.3.3.3
if (called) return;
called = true;
reject(r);
}
);
} else {
// PromisesA+ 2.3.3.4
resolve(x);
}
} catch (e) {
// PromisesA+ 2.3.3.2 | PromisesA+ 2.3.3.3.3 | PromisesA+ 2.3.3.3.4
if (called) return;
called = true;
reject(e);
}
} else {
// PromisesA+ 2.3.4
resolve(x);
}
}
}
// @ts-ignore
MyPromise.deferred = function () {
let deferred: any = {};
deferred.promise = new MyPromise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
};
// @ts-ignore
// export = MyPromise;