Easy Explanation
Explanation:Input Handling:The function receives an array of asynchronous functions that return promises.Result Storage:Use an array results to store the resolv
# Explanation:
## Input Handling:

The function receives an array of asynchronous functions that return promises.
## Result Storage:

Use an array results to store the resolved values in the correct order.
Use a completed counter to track how many promises have successfully resolved.
## Iteration:

For each function in the input array, call the function to execute its promise.
Use .then to handle successful resolution and .catch for rejections.
## Resolve on Success:

Increment the completed counter for every resolved promise.
When all promises have resolved (completed === functions.length), resolve the main promise with the results array.
## Reject on Failure:

If any promise is rejected, reject the main promise immediately with the rejection reason.
## Avoid Duplicate Rejections:

Use a hasRejected flag to ensure only the first rejection triggers the rejection of the main promise.

# Code
```javascript []
/**
 * @param {Array<Function>} functions
 * @return {Promise<any>}
 */
var promiseAll = function(functions) {
    return new Promise((resolve, reject) => {
        let results = [];
        let completed = 0;
        let hasRejected = false;

        functions.forEach((fn, index) => {
            fn()
                .then((value) => {
                    if (hasRejected) return;

                    results[index] = value; // Store the resolved value in the correct order
                    completed++;

                    // If all promises are resolved, resolve the main promise
                    if (completed === functions.length) {
                        resolve(results);
                    }
                })
                .catch((error) => {
                    if (!hasRejected) {
                        hasRejected = true; // Mark as rejected to avoid duplicate calls
                        reject(error); // Reject the main promise immediately
                    }
                });
        });
    });
};

/**
 * Example Usage:
 * const promise = promiseAll([
 *   () => new Promise(res => setTimeout(() => res(42), 100)),
 *   () => new Promise(res => setTimeout(() => res(24), 200))
 * ]);
 * promise.then(console.log); // [42, 24]
 * 
 * const promiseWithError = promiseAll([
 *   () => new Promise(res => setTimeout(() => res(42), 100)),
 *   () => new Promise((_, rej) => setTimeout(() => rej("Error"), 50))
 * ]);
 * promiseWithError.catch(console.error); // "Error"
 */

```
['JavaScript']