Javascript easy solution
IntuitionThe problem requires executing an array of asynchronous functions in parallel and resolving when all succeed or rejecting when any fails.
The first tho
## **Intuition**
The problem requires executing an array of asynchronous functions in parallel and resolving when all succeed or rejecting when any fails.  
- The first thought is to initiate all promises simultaneously.  
- Since JavaScript's `Promise.all` does this internally, we need to implement a custom version of it.
- We need a mechanism to track the resolution of all promises while also catching failures early.

---

## **Approach**
1. **Initialize tracking variables**  
   - Create an array `results` to store resolved values.
   - Create a counter `resolvedCount` to track how many promises have resolved.
   - Return a new `Promise`, resolving only when all async functions complete successfully.

2. **Execute all functions in parallel**  
   - Iterate through the array of functions and call each one to get a promise.
   - Attach `.then()` to store resolved values at the correct index.
   - Attach `.catch()` to reject the main promise immediately if any promise fails.

3. **Manage resolution and rejection**  
   - If all promises resolve successfully, the main promise resolves with the collected values.
   - If any promise rejects, the main promise rejects immediately with the first encountered rejection reason.

---

**Time Complexity:**   **O(n)**

---

**Space Complexity:** **O(n)**

---


## **Example Walkthrough**
### **Example 1**
```javascript
const functions = [
  () => new Promise(resolve => setTimeout(() => resolve(5), 200))
];

promiseAll(functions).then(console.log).catch(console.error);
// Output after 200ms: [5]
```

### **Example 2**
```javascript
const functions = [
  () => new Promise(resolve => setTimeout(() => resolve(1), 200)), 
  () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))
];

promiseAll(functions).then(console.log).catch(console.error);
// Output after 100ms: "Error"
```

### **Example 3**
```javascript
const functions = [
  () => new Promise(resolve => setTimeout(() => resolve(4), 50)), 
  () => new Promise(resolve => setTimeout(() => resolve(10), 150)), 
  () => new Promise(resolve => setTimeout(() => resolve(16), 100))
];

promiseAll(functions).then(console.log).catch(console.error);
// Output after 150ms: [4, 10, 16]
```

# Code
```javascript
/**
 * @param {Array<Function>} functions
 * @return {Promise<any>}
 */
var promiseAll = function (functions) {
    return new Promise((res, rej) => {
        let count = 0;
        let ans = new Array(functions.length);

        functions.forEach((fn, index) => {
            fn().then((data) => {
                ans[index] = data;

                count++;
                if (count === functions.length) {
                    res(ans);
                }
            }).catch((err) => {
                rej(err);
            })
        })
    })
};

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