Solution with async/await
Complexity\n\n\n\n# Solution\n\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n    \n    re
# Complexity\n\n![\u0421\u043D\u0438\u043C\u043E\u043A \u044D\u043A\u0440\u0430\u043D\u0430 2023-09-17 \u0432 19.56.09.png](https://assets.leetcode.com/users/images/aafbca44-07c2-4df4-b0d5-b6425254e164_1694969859.9677656.png)\n\n# Solution\n```\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n    \n    return new Promise( async(resolve,reject) => {\n        let count = 0;\n        let arr = []\n            for(let i = 0;i<functions.length;i++) {\n            \n            (async function(i) {\n                try {\n                let res = await functions[i]()\n                count++;\n                arr[i] = res\n                if (count === functions.length) resolve(arr);\n                } catch(err) {\n                    reject(err)\n                }\n            })(i)\n            \n        }\n    })\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */\n```\n# first approach\nThe first idea was to use ***async/await*** with a for loop.\nThe first approach looked like this:\n\n```\nvar promiseAll = function(functions) {\n    return new Promise(async (resolve, reject) => {\n        let answers = []\n\n        for(let i = 0; i < functions.length; i++) {\n            try {\n                let res = await functions[i]();\n                answers.push(res);\n            } catch(err) {\n                reject(err);\n            }\n        }\n        resolve(answers);\n    });\n};\n\n```\n# Problems with the first approach code\nI\'m using `answers.push(res);` and `resolve(answers);`, this is a bad decision because of the asynchronous nature of javascript.\n- promises might not be resolved, but we are calling `resolve(answers);` which will return an empty array\n- In `answers.push(res);` we are depending on the positions of elements in an array, though we need to return them in positions like in functions array.\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n# Second Approach\nI tried using `let count = 0;` to attach values to the array by the index  when they were executed. Also, I added `if(count === functions.length) resolve(arr)` to make sure that I resolve promise only when all promises are in array\n\n```\nvar promiseAll = function(functions) {\n    return new Promise( async(resolve,reject) => {\n        let count = 0;\n        let arr = []\n\n        try {\n            for(let i = 0;i<functions.length;i++) {\n            \n            let res =  await functions[i]()\n            count++\n            arr[i] = res;\n\n            if(count === functions.length) resolve(arr)\n            \n        }\n        } catch(err) {\n            reject(err)\n        }\n    })\n};\n```\n\n# Problems with second approach code\nThis attempt was better, however i forgot about the asynchronous value `i`\n and that by the time promise resolves, loop has already moved to a new cycle of iteration, causing i to be different\n# Final approach\nTo capture the correct value of `i` we can use ***async IIFE***\nread about [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE), if you don\'t know.\n\n# Final solution\n```\n/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n    \n    return new Promise( async(resolve,reject) => {\n        let count = 0;\n        let arr = []\n            for(let i = 0;i<functions.length;i++) {\n            \n            (async function(i) {\n                try {\n                let res = await functions[i]()\n                count++;\n                arr[i] = res\n                if (count === functions.length) resolve(arr);\n                } catch(err) {\n                    reject(err)\n                }\n            })(i)\n            \n        }\n    })\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */\n```\n\n\n\n
['JavaScript']