Promise.all((Promise.then(1), 2).then(v=> console.log(v))) // 输出1,2
Promise.all((Promise.then(1), 2, Promise.reject(3)).then(v=> console.log(v))) // 3
Promise.all((setTimeout()=>{console.log(1), 500}, 2, Promise.reject(3)).then(v=> console.log(v))) // 1,2,3
Promise.all = function (arr) {
let index = 0,
result = [];
return new Promise((resolve, reject) => {
arr.forEach((p, i) => {
Promise.resolve(p).then(
(val) => {
index++;
result[i] = val;
if (index === arr.length) {
resolve(result);
}
},
(err) => {
reject(err);
}
);
});
});
};