My Solution
Approach\nThe promiseAll function is designed to behave similarly to Promise.all(). It takes an array of functions that return promises (functions), waits for a
# Approach\nThe promiseAll function is designed to behave similarly to Promise.all(). It takes an array of functions that return promises (functions), waits for all of them to resolve, and then returns an array of their results in the same order as the input functions. If any of the promises reject, promiseAll will reject with the first encountered error.\n\nStep-by-Step Breakdown:\nReturn a New Promise:\n\n\n```\nreturn new Promise((res, rej) => {\n```\nThe promiseAll function returns a new Promise.\nThe res (resolve) and rej (reject) functions are the callbacks used to either resolve or reject this promise.\nInitialize Results Array and Counter:\n```\nlet results = new Array(functions.length);\nlet count = 0;\n```\nresults is an array initialized with the same length as functions, which will store the resolved values of each promise.\ncount keeps track of how many promises have been fulfilled.\nLoop Through Each Function:\n```\nfunctions.forEach((fn, i) => {\n```\nforEach is used to iterate over each function in the functions array.\nfn is the current function being processed, and i is its index in the array.\nExecute Each Function (Returning a Promise):\n```\nfn()\n```\nEach function (fn) is executed. Since these functions return promises, this starts the promise chain.\nHandle Promise Resolution:\n```\n.then(val => {\n    results[i] = val;\n    count++;\n    if (count === functions.length) res(results);\n})\n```\n.then(val => { ... }): The then clause is triggered when the promise returned by fn() is fulfilled.\nresults[i] = val;: The resolved value (val) is inserted into the results array at the correct index (i).\ncount++;: The count variable is incremented to track how many promises have been fulfilled.\nif (count === functions.length) res(results);: If count equals the total number of functions (i.e., all promises have been fulfilled), the results array is resolved using res(results).\nHandle Promise Rejection:\n\n```\n.catch(err => rej(err))\n```\nIf any of the promises reject, the catch clause is triggered.\nrej(err): The whole promiseAll function is rejected with the error err.\n# **Key Concepts:**\n- Order Preservation: The results[i] = val; line ensures that the order of the resolved values in the results array matches the order of the input functions, regardless of the order in which the promises are fulfilled.\n- Tracking Fulfillment: The count variable tracks how many promises have been fulfilled. Only when count equals the length of the functions array does the promiseAll function resolve with the results array.\n- Immediate Rejection: If any promise in the array rejects, the promiseAll function rejects immediately, ensuring that you don\u2019t get partial results.\n\n# Complexity\n- Time complexity:\n  O(N)\n\n\n# Code\n```javascript []\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = async function(functions) {\n    return new Promise((res, rej)=>{\n        let results = new Array(functions.length);\n        let count = 0;\n        functions.forEach((fn, i)=>{\n            fn(). //fn is the current function being processed, and i is its \n                    // index in the array. fn return promise\n            then(val=> {\n                results[i] = val; \n//ensures that the order of the resolved values in the results array matches the order of the input functions, \n//regardless of the order in which the promises are fulfilled.\n                count++;\n                if(count===functions.length)//all promises resolved\n                res(results);\n            })\n            .catch(err => rej(err))\n        });\n    });\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */\n```
['JavaScript']