Promise A+ 规范
Published in:2021-03-17 | category: 前端 面试

Promise 对象用于表示一个异步操作的最终完成(或失败)及其结果值。

Promise A+ 规范

Promise A+ 规范

手写 Promise

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
const PENDING = 'pending'
const RESOLVED = 'resolved'
const REJECTED = 'rejected'

const asyncFun = (fn) => {
setTimeout(fn, 0)
}

class Promise_ {

status;
resolver;
rejecter;

constructor(executor, options) {
this.status = PENDING;
executor(res => {
asyncFun(() => {
this.status = RESOLVED;
this.resolver?.(res);
});
}, err => {
asyncFun(() => {
this.status = REJECTED;
this.rejecter?.(err);
asyncFun(() => {
throw Error(err);
})
})
})
}

then(resolver, rejecter) {
this.resolver = resolver;
this.rejecter = rejecter;
}

static reject(err) {
return new Promise_((resolve, reject) => {
reject(err);
})
}

static resolve(value) {
if(value instanceof Promise_) return value;
return new Promise_((resolve) => {
resolve(value);
})
}

static all(promisers) {
return new Promise_((resolve, reject) => {
let count = 0;
const resArr = [];
const resolver = (res, index) => {
resArr[index] = res;
count++;
if(count === promisers.length) {
resolve(resArr);
}
};
const rejecter = (err) => {
reject(err)
};
promisers.map((promiser, index) => {
promiser.then(
res => resolver(res, index),
err => rejecter(err)
);
})
})
}

static race(promisers) {
return new Promise_((resolve, reject) => {
let flag = false;
const resolver = res => {
if(!flag) {
flag = true;
resolve(res);
};
};
const rejecter = err => {
reject(err)
};

promisers.map(promiser => {
promiser.then(
resolver,
rejecter
);
})
})
}

static any(promisers) {
return new Promise_((resolve, reject) => {
let stopReject = false;
const resolver = res => {
if(!stopReject) {
stopReject = true;
resolve(res);
};
};

const errs = [];
let count = 0;
const rejecter = err => {
if(stopReject) return;
count++;
errs.push(err);
if(count === promisers.length) {
reject(errs);
asyncFun(() => {
throw new AggregateError(errs)
});
}

};

promisers.map(promiser => {
promiser.then(
resolver,
rejecter
);
})
})
}
}

const promise1 = new Promise_((resolve, reject) => {
setTimeout(() => {
resolve(1)
// reject('发生错误1')
}, 100)
})

const promise2 = new Promise_((resolve, reject) => {
setTimeout(() => {
resolve(2)
// reject('发生错误2')
}, 50)
})

const promise3 = new Promise_((resolve, reject) => {
setTimeout(() => {
resolve(3)
// reject('发生错误3')
}, 0)
})

Promise_.race([promise1, promise2, promise3]).then(res => {
console.log(res);
}, err => {
console.log(err);
})

const promise4 = Promise_.resolve(promise3);
promise4.then(res => {
console.log(res);
})

const promise5 = Promise_.reject(new Error('fail'));

promise5.then(
() => {},
(err) => {
console.log(err);
}
);


console.log(5);



Prev:
Chrome 性能监测
Next:
现代浏览器内部揭秘