🍞🍞🍞 Not familiar with promises? FULL thorough explanation || ✅ beginner friendly
Intuition\nIf you are familiar with promises, this seems intuitive enough.\n\nCreate an array of function results, and set resolve to that array. If an error is
# Intuition\nIf you are familiar with promises, this seems intuitive enough.\n\nCreate an array of function results, and set ```resolve``` to that array. If an error is caught, set ```reject``` to the first error that is encountered.\n\n# Promises\nIf you are not familiar with promises this section is a detailed explanation. Promises are how JavaScript carries out asynchronous functions.\n\n- Asynchronous functions can run in the background while the application linearly runs other functions. For example, let\'s say a user uploads tons of data and it will take forever to load. This data should be uploaded asynchronously, so that the user can continue to do other things, until the function(s) uploading the data finishes successfully. To emphasize, code generally runs line by line\u2014in order. Async functions run in the background, and **completely out of sync** from the usual linear order.\n- Promises return the resulting value(s) of the async function(s) in two ways. If everything was carried out successfully, we say that the the function(s) resolved successfully, and we call the promise\'s ```resolve``` function, passing the results of the resolved function(s) as a parameter. If an error occurred, the function was rejected, and we call the ```reject``` function, passing in the error as a parameter.\n- We can call a function in a promise the following way:\n    ```\n    fn()\n    .then((result) => { resolve(result) })\n    .catch((err) => { reject(err) });\n    ```\n    When we want to execute a function ```fn``` in a promise, we can write a line of form ```fn().then().catch()```. If the executed function resolves, the program does everything in ```.then()```. If an error is thrown, the function does everything in ```.catch()```.\n\n# Approach\n1. Initialize ```arr``` of size ```functions.length```\n2. Create variable ```waitingFor``` and set it to ```functions.length```. This represents the number of functions we are waiting to resolve. When ```waitingFor``` is 0, this means all functions have been resolved, and we call ```resolved```.\n3. Iterate through ```functions```, executing each function. If the function resolves, set ```arr[i]``` to the result of the function, and decrement ```waitingFor```. If an error ```err``` is returned, call ```reject(err)```.\n    - Note, in the ```.catch()``` portion of the solution code, I simply wrote ```.catch(reject)```. Since I am doing nothing in ```.catch()``` except calling ```reject```, I can simply call it without explicitly defining the error parameter. The ```catch``` call will automatically take any errors it recieved and pass it to the ```reject``` call.\n4. When ```waitingFor``` is 0, that means all functions have resolved, and there are no errors. Call ```resolve(arr)```.\n\n# Code\n```\nvar promiseAll = async function(functions) {\n    return await new Promise((resolve, reject) => {\n        // steps 1 and 2 of approach described above\n        let arr = Array(functions.length);\n        let waitingFor = functions.length;\n\n        // steps 3 and 4\n        for(let i = 0; i < functions.length; ++i){\n            functions[i]()\n                .then((result) => { \n                    arr[i] = result; \n                    if (--waitingFor === 0) resolve(arr);\n                }).catch(reject);\n        }\n    });\n};\n```\nUPVOTE if this was helpful \uD83C\uDF5E\uD83C\uDF5E\uD83C\uDF5E
['Array', 'Concurrency', 'JavaScript']