Imagine you're a concert conductor who needs to coordinate multiple musicians to play their parts simultaneously. In the programming world, you're tasked with executing multiple asynchronous functions in parallel and collecting their results.
Given an array of asynchronous functions, you need to create a new promise that:
- โ Resolves when all functions complete successfully, returning an array of results in the same order as the input functions
- โ Rejects immediately when any function fails, with the reason from the first rejection
๐ซ Challenge: You cannot use the built-in Promise.all() function - you must implement this functionality from scratch!
Example: If you have functions that resolve to ["hello", "world", "!"], your promise should resolve to ["hello", "world", "!"] in exactly that order, even if the functions complete in a different sequence.
Input & Output
Visualization
Time & Space Complexity
Total time is only as long as the slowest promise since they run in parallel
Need to store results array and track state for n promises
Constraints
- 1 โค functions.length โค 103
- Each function returns a Promise
- Functions accept no arguments
- Cannot use built-in Promise.all()
- Must maintain original order in results
- Must reject immediately on first failure