2721. Execute Asynchronous Functions in Parallel
IntuitionApproachExplanation:
Initialization:

Initialized results as an empty array and completedCount to keep track of resolved promises.

Edge Case:

If func
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**Explanation:**\n1. Initialization:\n- Initialized results as an empty array and completedCount to keep track of resolved promises.\n\n2. Edge Case:\n- If functions array is empty, it immediately resolves with an empty array.\n\n3. Execution and Result Collection:\n- Used forEach to iterate over functions, calling each function and storing results in the correct index.\n- Incremented completedCount after each successful resolution. Once all functions are resolved, the main promise resolves with the results array.\n\n4. Error Handling:\n- Directly passed reject in the catch block for simplicity.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n    return new Promise((resolve, reject) => {\n        let results = []\n        let counter = 0\n\n        if (functions.length === 0) {\n            resolve(results)\n        }\n\n        for (let i = 0; i < functions.length; i++) {\n            functions[i]()\n                .then(result => {\n                    results[i] = result\n                    counter++\n\n                    if (counter === functions.length) {\n                        resolve(results)\n                    }\n                })\n                .catch(reason => {\n                    reject(reason)\n                })\n        }\n    })\n};\n```
['JavaScript']