const myPromiseAll = function (iterable) {
const ret = []
const errors = []
return new Promise((resolve, reject) => {
for (let eachPromise of iterable) {
Promise.resolve(eachPromise).then((value) => {
ret.push(value)
if (ret.length === iterable.size || ret.length === iterable.length) {
resolve(ret)
}
}).catch(e => {
errors.push(e)
if (errors.length === 2) {
reject(errors)
}
})
}
})
}
myPromiseAll([Promise.reject(1), Promise.resolve(2), Promise.resolve(3),
new Promise(resolve => setTimeout(resolve, 1000, 1))])
.then((value) => console.log(value))
.catch(e =>console.log(e))
console