Optimal approach
Approach\n- Create a New Promise:\n\n    - The promiseAll function encapsulates the logic within a new Promise, where the resolution or rejection of the promise
# Approach\n- Create a New Promise:\n\n    - The promiseAll function encapsulates the logic within a new Promise, where the resolution or rejection of the promise depends on the execution of all the input functions.\n- Initialize Result Array and Counter:\n\n    - An array is created to store the results of the resolved promises in the order corresponding to the input functions.\n    - A counter (waitingFor) is initialized to track the number of pending promises. This ensures the final promise resolves only when all promises are resolved.\n- Iterate Over Functions:\n\n    - Loop through each function in the input array, invoke the function to get a promise, and attach .then and .catch handlers to handle the resolution or rejection of each promise.\n- Handle Promise Resolution:\n\n    - When a promise resolves, its result is stored in the result array at the same index as its corresponding function in the input array.\n    - The counter is decremented to indicate one less promise is pending.\n    - When the counter reaches zero, the result array is passed to the resolve function of the main promise, resolving it.\n- Handle Promise Rejection:\n\n    - If any promise rejects, the reject function of the main promise is called immediately with the rejection reason. This short-circuits the execution, and the remaining promises are ignored.\n- Order Preservation:\n\n    - The results are stored in the same order as the input functions, regardless of the order in which the promises resolve or reject.\n# Code\n```typescript []\ntype Fn<T> = () => Promise<T>\n\nfunction promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {\n    return new Promise((resolve,reject) => {\n        let res = Array(functions.length);\n        let waitingFor = functions.length;\n\n        for(let i = 0; i < functions.length; i++){\n            functions[i]()\n            .then((result) => {\n                res[i] = result;\n                waitingFor--;\n                if(waitingFor === 0) resolve(res);\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```
['TypeScript']