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
| // TypeScript实现
export class PfPromise {
private status: 'pending' | 'resolved' | 'rejected' = 'pending';
private value: any;
private onResolvedCallbacks: Array<(value: any) => void> = [];
private onRejectedCallbacks: Array<(reason: any) => void> = [];
constructor(
executor: (
resolve: (value: any) => void,
reject: (reason: any) => void
) => void
) {
try {
executor(this.resolve.bind(this), this.reject.bind(this));
} catch (error) {
this.reject(error);
}
}
private resolve(value: any) {
if (value instanceof PfPromise) {
return value.then(this.resolve.bind(this), this.reject.bind(this));
}
setTimeout(() => {
if (this.status === 'pending') {
this.status = 'resolved';
this.value = value;
this.onResolvedCallbacks.forEach((callback) => callback(value));
}
});
}
private reject(reason: any) {
setTimeout(() => {
if (this.status === 'pending') {
this.status = 'rejected';
this.value = reason;
this.onRejectedCallbacks.forEach((callback) => callback(reason));
}
});
}
then(onFulfilled?: (value: any) => any, onRejected?: (reason: any) => any) {
return new PfPromise((resolve, reject) => {
const fulfilledCallback = () => {
try {
const result = onFulfilled ? onFulfilled(this.value) : this.value;
resolvePromise(result, resolve, reject);
} catch (error) {
reject(error);
}
};
const rejectedCallback = () => {
try {
const result = onRejected ? onRejected(this.value) : this.value;
resolvePromise(result, resolve, reject);
} catch (error) {
reject(error);
}
};
if (this.status === 'resolved') {
setTimeout(fulfilledCallback);
} else if (this.status === 'rejected') {
setTimeout(rejectedCallback);
} else {
this.onResolvedCallbacks.push(fulfilledCallback);
this.onRejectedCallbacks.push(rejectedCallback);
}
});
}
catch(onRejected: (reason: any) => any) {
return this.then(undefined, onRejected);
}
static all(promises: PfPromise[]) {
return new PfPromise((resolve, reject) => {
const results: any[] = [];
let completed = 0;
promises.forEach((promise, index) => {
promise
.then((data) => {
results[index] = data;
completed++;
if (completed === promises.length) {
resolve(results);
}
})
.catch(reject);
});
});
}
static race(promises: PfPromise[]) {
return new PfPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(resolve).catch(reject);
});
});
}
static resolve(value: any) {
return new PfPromise((resolve) => resolve(value));
}
static reject(reason: any) {
return new PfPromise((_, reject) => reject(reason));
}
}
function resolvePromise(
x: any,
resolve: (value: any) => void,
reject: (reason: any) => void,
visited = new Set<any>()
) {
if (visited.has(x)) {
return reject(new TypeError('循环引用'));
}
if (x instanceof PfPromise) {
visited.add(x);
x.then(
(value) => resolvePromise(value, resolve, reject, visited),
(reason) => reject(reason)
);
} else if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
let then;
try {
then = x.then;
} catch (error) {
return reject(error);
}
if (typeof then === 'function') {
let called = false;
try {
visited.add(x);
then.call(
x,
(y) => {
if (called) return;
called = true;
resolvePromise(y, resolve, reject, visited);
},
(r) => {
if (called) return;
called = true;
reject(r);
}
);
} catch (error) {
if (!called) {
reject(error);
}
}
} else {
resolve(x);
}
} else {
resolve(x);
}
}
|