Beats 100% of TypeScript users in Memory
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nI\'m not using a counter to decide whether all the Promises are resolved 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nI\'m not using a counter to decide whether all the Promises are resolved or not. Instead I\'m comparing my ```functionsResults``` array with the input ```functions``` array.\nThe problem is that comparing the length will not work as adding an element at index ```3``` of an empty array will result of a length of ```4```, with values of previous indexes set to ```null```.\nSo before comparing the length I\'m filtering my ```functionsResults``` array.\n\nHOWEVER, it\'s not a good practice at all. Since a Promise can indeed resolve a ```null``` value.\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```\ntype Fn<T> = () => Promise<T>\n\nfunction promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {\n    const functionsResults: T[] = [];\n\n    return new Promise((resolve, reject) => {\n        for (let i = 0; i < functions.length; i++) {\n            functions[i]().then((result) => {\n                functionsResults[i] = result;\n\n                if (functionsResults.filter(_ => _ !== null).length === functions.length) {\n                    resolve(functionsResults);\n                }\n            }).catch(reject);\n        }\n    });\n};\n```
['TypeScript']