Promise.all2 = function (iterators) {
return new Promise((resolve, reject) => {
if (!iterators || iterators.length === 0) {
resolve([])
} else {
let count = 0;
let result = [];
let len = iterators.length
for (let i = 0; i < len; i++) {
Promise.resolve(iterators[i]).then((data) => {
result[i] = data;
if (++count === len) {
resolve(result);
}
}, (err) => {
reject(err);
})
}
}
})
}
const f1 = new Promise((resolve, reject) => {
console.log('enter f1')
resolve('f1')
})
const f2 = new Promise((resolve, reject) => {
console.log('enter f2')
reject('err')
})
const f3 = new Promise((resolve, reject) => {
console.log('enter f3')
resolve('f3')
})
Promise.all2([f1, f2, f3]).then(data => {
console.log(data)
}).catch(err => {
console.log(err)
})
console