Execute Asynchronous Functions in Parallel sort javascript solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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 []\nfunction promiseAll(functions) {\n    return new Promise((resolve, reject) => {\n        const results = new Array(functions.length);\n        let completedPromises = 0;\n        let hasRejected = false;\n        let firstRejectReason;\n\n        functions.forEach((func, index) => {\n            func().then(result => {\n                if (!hasRejected) {\n                    results[index] = result; // Store the resolved value\n                    completedPromises++;\n\n                    // Check if all promises have completed\n                    if (completedPromises === functions.length) {\n                        resolve(results); // Resolve with all results\n                    }\n                }\n            }).catch(error => {\n                if (!hasRejected) {\n                    hasRejected = true; // Mark as rejected\n                    firstRejectReason = error; // Store the first rejection reason\n                    reject(firstRejectReason); // Reject with the error\n                }\n            });\n        });\n    });\n}\n\n// Example usage:\nconst start = Date.now();\n\nconst functions1 = [\n    () => new Promise(resolve => setTimeout(() => resolve(5), 200))\n];\n\npromiseAll(functions1).then(result => {\n    console.log({"t": Date.now() - start, "resolved": result});\n}).catch(error => {\n    console.log({"t": Date.now() - start, "rejected": error});\n});\n\nconst functions2 = [\n    () => new Promise(resolve => setTimeout(() => resolve(1), 200)),\n    () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))\n];\n\npromiseAll(functions2).then(result => {\n    console.log({"t": Date.now() - start, "resolved": result});\n}).catch(error => {\n    console.log({"t": Date.now() - start, "rejected": error});\n});\n\nconst functions3 = [\n    () => new Promise(resolve => setTimeout(() => resolve(4), 50)),\n    () => new Promise(resolve => setTimeout(() => resolve(10), 150)),\n    () => new Promise(resolve => setTimeout(() => resolve(16), 100))\n];\n\npromiseAll(functions3).then(result => {\n    console.log({"t": Date.now() - start, "resolved": result});\n}).catch(error => {\n    console.log({"t": Date.now() - start, "rejected": error});\n});\n```
['JavaScript']