Solution with simple explanation.
Firstly, I want to say that I've got this task at the first part of a technical interview to Yandex. And I've managed to solve it). That's why I've decided to s
Firstly, I want to say that I've got this task at the first part of a technical interview to Yandex. And I've managed to solve it). That's why I've decided to share my solution.

# Intuition
The main thing from which we should start is that our function must return a Promise. So question can be narrowed to the idea of how to resolve and reject our Promise.

# Approach
So let's dig deeper. We should resolve promise only when all functions are resolved successfully and return results in the same order. To achieve this, we created the array and set its length to the length of functions. Then, in the loop, we iterate over functions, execute them, and assign the result according to its index. We also need a counter, and when it's equal to functions length, that means we can resolve our promise. And the last part - rejecting the promise. We should do it in any case of reject in our functions array. So we can simply reject at our catch function. And that's all!


# Complexity
- Time complexity:  $$O(n)$$

- Space complexity: $$O(n)$$

# Code
```javascript []
/**
 * @param {Array<Function>} functions
 * @return {Promise<any>}
 */
var promiseAll = function(functions) {
    return new Promise((resolve, reject) => {
        let result = new Array(functions.length);
        let counter = 0;
        for (let i = 0; i < functions.length; i++) {
            functions[i]().then(res => { 
                result[i] = res;
                counter++;
                if (counter == functions.length) {
                    resolve(result);
                }
            }).catch(err => reject(err));
        }
    });
};

/**
 * const promise = promiseAll([() => new Promise(res => res(42))])
 * promise.then(console.log); // [42]
 */
```
['JavaScript']