JavaScript Solution | Execute Asynchronous Functions in Parallel
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to execute multiple asynchronous functions concurrently and handle their result
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to execute multiple asynchronous functions concurrently and handle their results collectively. The key is to keep track of the number of completed functions and resolve or reject the main promise accordingly.\n\n1. `promise resolves`\n\n    - when all asynchronous functions in the array have successfully completed in parallel, and the resolved value is an array containing the values of each resolved promise in the order they were in the original array.\n\n2. `promise rejects`\n\n    - when any promise returned by the functions is rejected, the main promise also rejects with the reason of the first encountered rejection.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. These are comment delimiters for a multi-line comment to help explain and document code while preventing ignored text from executing. These comments are commonly used in formal documentation for better code understanding.\n\n    ```\n    /**\n    * Multi-line comment\n    */\n    ```\n\n2. This is a special type of comment called a JSDoc comment that explains that the function should take a parameter named `functions`, which is expected to be an `Array` of `functions`.\n\n    ```\n    @param {Array<Function>} functions\n    ```\n\n3. This is a special type of comment called a JSDoc comment that explains what the code should return, which, in this case, is a `Promise` that will resolve to an array of results or reject with an error. It helps developers and tools like code editors and documentation generators know what the function is supposed to produce.\n\n    ```\n    @return {Promise<any>}\n    ```\n\n4. This is how we define a function named `promiseAll` that takes an array of `functions` as its argument.\n\n    ```\n    var promiseAll = function(functions) {\n        // code to be executed\n    };\n    ```\n\n5. This is how we can create a new `Promise` with `resolve` and `reject` functions as parameters.\n\n    ```\n    return new Promise((resolve, reject) => {\n        // code to be executed\n    })\n    ```\n\n6. This creates an array named `results` to store the results of the asynchronous functions.\n\n    ```\n    let results = new Array(functions.length);\n    ```\n\n7. This is how we can initialize a counter named `count` to track the number of completed functions.\n\n    ```\n    let count = 0;\n    ```\n\n8. This function executes an array of asynchronous functions concurrently, collects their results, and resolves a main promise with the array of results when all functions have completed successfully, or rejects with the first encountered error.\n\n    ```\n    functions.forEach((fn, i) => {\n        fn()\n        .then(val => {\n            results[i] = val;\n            count++;\n            if(count === functions.length) resolve(results);\n        })\n        .catch(error => reject(error));\n    })\n    ```\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of functions in the input array. This is because the function iterates over each function in the array and calls it, which takes constant time.\n\n- Space complexity: $O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n`n` is the number of functions in the input array. This is because the function uses space proportional to the number of functions by creating an array and variables for each iteration of the loop.\n\n# Code\n```\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n    return new Promise((resolve, reject) => {\n        let results = new Array(functions.length);\n        let count = 0;\n        functions.forEach((fn, i) => {\n            fn()\n            .then(val => {\n                results[i] = val;\n                count++;\n                if(count === functions.length) resolve(results);\n            })\n            .catch(error => reject(error));\n        })\n    })\n};\n```
['JavaScript']