0%

polyfill

Promise.all

1
2
3
4
5
6
7
8
9
10
11
12
13
Promise.all = function (promises) {
let results = [];
return new Promise((resolve, reject) => {
promises.forEach((p, index) => {
p.then((result) => {
results.push(result);
if (index === promises.length - 1) {
resolve(results);
}
}).catch((err) => reject(err));
});
});
};

Promise.any

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Promise.any = function (promises) {
const result = [];
return new Promise((resolve, reject) => {
promises.forEach((item, index) => {
item
.then((res) => resolve(res))
.catch((error) => {
result.push(error);
if (index === pro mises.length - 1) {
// 如果全部都失败,返回结果合集
reject(result);
}
});
});
});
};