Problem Description
Given an array of asynchronous functions, return a new promise. Each function in the array accepts no arguments and returns a promise. All the promises should be executed in parallel. The promise resolves when all the promises returned from functions were resolved successfully in parallel, returning an array of all the resolved values. The promise rejects when any of the promises were rejected, with the reason of the first rejection.
Key Insights
- Each function in the array returns a promise that needs to be executed.
- All promises must be executed in parallel.
- The final promise must resolve with an array of results or reject with the reason of the first failure.
- We cannot use the built-in Promise.all function; we need to manage promise execution manually.
Space and Time Complexity
Time Complexity: O(n) - where n is the number of functions, as we initiate all promises simultaneously. Space Complexity: O(n) - for storing the results of the promises in an array.
Solution
To solve the problem, we will:
- Create a new promise that will handle the execution of the asynchronous functions.
- Utilize an array to store the results of the resolved promises.
- Keep track of the number of promises resolved and the first rejection encountered.
- For each function, invoke it and attach
.then()
and.catch()
handlers. - If all promises resolve, the final promise resolves with the array of results; if any promise rejects, it rejects immediately with the reason of that rejection.