← 返回首页
Promise.all 简易实现
Promise.myAll = function(iterable) {
return new Promise((resolve, reject) => {
const arr = Array.from(iterable);
if (arr.length === 0) return resolve([]);
const res = new Array(arr.length);
let count = 0;
arr.forEach((p, i) => {
Promise.resolve(p)
.then(val => {
res[i] = val;
count++;
if (count === arr.length) resolve(res);
})
.catch(reject);
});
});
};