Parallel Promise Executer
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
function promiseAll(functions) {
  return new Promise((resolve, reject) => {
    const results = new Array(functions.length); // To store results in the correct order
    let completed = 0; // To track the number of completed promises

    functions.forEach((fn, index) => {
      fn()
        .then((value) => {
          results[index] = value; // Store the result at the correct index
          completed += 1;

          // If all functions have resolved, resolve the promise
          if (completed === functions.length) {
            resolve(results);
          }
        })
        .catch((err) => {
          // Reject immediately if any promise fails
          reject(err);
        });
    });
  });
}

// Example usage:

// Example 1
const functions1 = [
  () => new Promise((resolve) => setTimeout(() => resolve(5), 200)),
];
promiseAll(functions1)
  .then((res) => console.log({ t: 200, resolved: res }))
  .catch((err) => console.log({ t: Date.now(), rejected: err }));

// Example 2
const functions2 = [
  () => new Promise((resolve) => setTimeout(() => resolve(1), 200)),
  () => new Promise((_, reject) => setTimeout(() => reject("Error"), 100)),
];
promiseAll(functions2)
  .then((res) => console.log({ t: Date.now(), resolved: res }))
  .catch((err) => console.log({ t: 100, rejected: err }));

// Example 3
const functions3 = [
  () => new Promise((resolve) => setTimeout(() => resolve(4), 50)),
  () => new Promise((resolve) => setTimeout(() => resolve(10), 150)),
  () => new Promise((resolve) => setTimeout(() => resolve(16), 100)),
];
promiseAll(functions3)
  .then((res) => console.log({ t: 150, resolved: res }))
  .catch((err) => console.log({ t: Date.now(), rejected: err }));

```
['JavaScript']