Simple Solution with explanation of the program and approach to solve the problem step by step
IntuitionUnderstand the given condition and think the logic to the given problem and the return value.ApproachWe just have to return a promise with resolve and
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Understand the given condition and think the logic to the given problem and the return value.

---


# Approach
<!-- Describe your approach to solving the problem. -->
We just have to return a promise with resolve and rejected value of `functions`.

---


# Problem Breakdown
We have a array of  asynchronous functions `functions`, return a new promise. Each function in the array accepts no arguments and returns a promise. All the promises should be executed in parallel.

We have to store all the resolved value in the array formate with its corresonding index.

---


# Program breakdown
***This a breakdown of below program***
- As per the question we have to return a promise so we use
`return new Promise((resolve,reject)=>{}` with reject and resolve argument pass to the arrow function.
- we need an Array to store the value and a variable to check the all functions in `functions` are over or not
`let result = new Array(functions.length)`is array with length of `function` array `let count = 0` is a variable we need.
- we have to execute all the function so we use `forEach` loop with a arrow function `functions.forEach((fn,i)=>{}`, where `functions` is a array `fn` is a function which return a promise, `i` is looping variable.
- Now we need to add the resolved element to a `result` array so we use `fn().then( val => { result[i] = val count++ if(count === functions.length ) resolve( result )})`, where `result[i] = val` add the returned value to `result` array, `i` is index value, `var` is a resolved value from `fn` function. `count++` increment the `count` value that represent this execution is over.
- `if(count === functions.length) resolve(result)` this line execute when the all function present in the `functions` is executed. when `count` reach the `functions.length` means all functiona are executed we need to return a `result` array which we store the result value which is resolved of promise we created.
- We resolved a promise, Now we need to reject value so we use `.catch(r => reject(r))`, where r is a rejected return from a `fn` so we reject it by passing `r` to `reject()`.
---

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The time complexity of the `promiseAll` function can be analyzed based on the number of functions provided in the input array. Each function is invoked once, and since each function returns a promise, the time complexity is O(n).
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The space complexity of the function is also O(n) due to the `result` array that is created to store the resolved values of the promises. This array has a length equal to the number of functions, thus requiring space proportional to n. 

---
# Code
```javascript []

var promiseAll = function(functions) {

    return new Promise((resolve,reject)=>{

        let result = new Array(functions.length)    // array to store result
        let count = 0       // variable to check when to resolve the promise

        functions.forEach((fn,i)=>{

            fn().then(              // resolved value of fn
                val => {
                    result[i] = val
                    count++
                    if(count === functions.length) resolve(result)  // return result in the array formet
                }
            )

            .catch(r => reject(r))      // rejected value of fn
        })
    })
};

```
['JavaScript']