Junior — Senior
Independent implementation of the Promise.all method (version 3)
livecode
Task condition
You are required to write a function that reproduces the behavior of the standard Promise.all. In the example, an array of numbers is formed, from which promises are created, each resolving after a delay depending on its index. Your task is to implement promiseAll, which takes an array of promises and returns a new promise that resolves when all the input promises successfully complete, or rejects upon the first error.
const test: number[] = Array.from(Array(5), (_, index) => index);
const promises = test.map((item, index) => {
return new Promise((resolve, reject) => setTimeout(() => resolve(index), index * 100));
});
// promises.push(new Promise((resolve, reject) => reject('MyError')));
const promiseAll = () => {
};
// promiseAll(promises).then(data => console.log(data));