function delay(value, ms) {
return new Promise(resolve => {
setTimeout(() => {
console.log(`resolved: ${value} (${ms}ms)`);
resolve(value);
}, ms);
});
}
 
Promise.myAll = arr => {
return new Promise((resolve, reject) => {
if (arr == null || typeof arr[Symbol.iterator] !== 'function') {
return reject(new TypeError('Argument is not iterable'));
}
 
const result = [];
let count = 0;
let i = 0;
 
for (const el of arr) {
const currentIndex = i++;
Promise.resolve(el)
.then(res => {
result[currentIndex] = res;
count++;
 
if (count === arr.length) resolve(result);
})
.catch(reject);
}
});
};
 
// ==========================
// ТЕСТЫ
// ==========================
 
// "=== Array test ==="
 
// Promise.myAll([
// delay(1, 1000), // самый медленный
// delay(2, 100), // самый быстрый
// delay(3, 500),
// ]).then(res => console.log('myAll result:', res));
 
Promise.myAll('fff')
.then(res => console.log('myAll:', res))
.catch(console.log);
 
// Promise.myAll([Promise.resolve(1), 2, Promise.resolve(3)]).then(res => console.log('myAll:', res));
 
// "=== Set test ==="
const set = new Set([Promise.resolve(10), 20, Promise.resolve(30)]);
// Promise.myAll(set).then(res => console.log("myAll (Set):", res)).catch(console.log);
 
// "=== Map test ==="
const map = new Map([['a', Promise.resolve(100)], ['b', Promise.resolve(200)]]);
// Promise.myAll(map).then(res => console.log("myAll (Map):", res)).catch(console.log);
 
// "=== Generator test ==="
function* gen() {
yield Promise.resolve('A');
yield 'B';
yield Promise.resolve('C');
}
 
// Promise.myAll(gen()).then(res => console.log("myAll (Generator):", res)).catch(console.log);
 
Click Run to execute JavaScript.
Code runs in an isolated environment.