Implementing Promise.all() in JavaScript
Intuition\n Describe your first thoughts on how to solve this problem. \nThe key idea behind Promise.all() is that it takes an array of promises and returns a n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe key idea behind Promise.all() is that it takes an array of promises and returns a new promise that:\n\nResolves to an array containing the results of each promise, in the same order as the input array.\nRejects immediately if any of the input promises reject.\nWe need to:\n\nTrack how many promises have resolved.\nEnsure the results are stored in the correct order, even if the promises resolve out of order.\nHandle any rejection by immediately rejecting the returned promise.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo solve this problem, we\'ll follow these steps:\n\nCreate a new promise that will eventually resolve or reject.\nInitialize an array to store the results of the resolved promises.\nTrack the number of remaining promises that need to resolve.\nLoop through each function in the input array, invoke it, and handle its promise:\nOn resolution, store the result at the correct index and decrease the count.\nOn rejection, immediately reject the main promise.\nIf all promises resolve successfully, resolve the main promise with the results array.\n## Key Points:\nWe use Array.prototype.forEach to iterate through the functions.\nFor each function, we call it to get a promise, then attach .then() and .catch() handlers.\nThe .then() handler stores the result and checks if all promises have resolved.\nThe .catch() handler rejects the main promise if any promise fails.\n\n\n# Complexity\n- Time complexity: O(n)\nThe function iterates over all the input functions once, so the time complexity is linear relative to the number of input functions, i.e., O(n).\nThe individual promises might take varying amounts of time to resolve, but the overall complexity is determined by the number of promises.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\nWe allocate an array of size n to store the results.\nAdditionally, the closure created inside the promise function also requires some space, but it\'s proportional to the input size.\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 result = Array(functions.length);\n        let totalFunction = functions.length;\n        functions.forEach((e, i) => {\n            e().then((res) => {\n                    result[i] = res;\n                    totalFunction--;\n                    if(totalFunction === 0) resolve(result)\n                })\n                .catch(reject);\n        });\n    });\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */\n```
['JavaScript']