const rand = () => Math.random() * 2000;
 
const p1 = new Promise(r => setTimeout(r, rand(), 'A'));
const p2 = new Promise(r => setTimeout(r, rand(), 'B'));
const p3 = new Promise(r => setTimeout(r, rand(), 'C'));
const p4 = new Promise((resolve, reject) => {
setTimeout(reject, rand(), 'reject');
});
const p5 = new Promise(r => setTimeout(r, rand(), 'D'));
 
function all(iterable) {
return new Promise((res, rej) => {
const promises = Array.from(iterable);
const result = [];
if (promises.length === 0) res([]);
promises.forEach((promise,index)=> {
promise.res(promise).then(value => {
result[index] = value;
if(++completed === promises.length) res(result)
},reason => reject(reason)
)
})
});
}
 
all([p1, p2, p3, p4, p5]).then(
value => console.log('1 >>>', value),
reason => console.log('2 >>>', reason)
);
 
Code will run automatically after changes.
Code runs in an isolated environment.