Promise.myAll = function (iterator) {
return new Promise((resolve, reject) => {
const ret = []
let resolvedCount = 0
for (let promise of iterator) {
Promise.resolve(promise).then((data) => {
resolvedCount++
ret.push(data)
if (resolvedCount === iterator.length || resolvedCount === iterator.size) {
resolve(ret)
}
}).catch(e => {
reject(e)
})
}
})
}
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'foo');
});
Promise.myAll(new Set([promise1, promise2, promise3])).then((data) => {
console.log(data)
}).catch(e => console.log(e))
console