Parallel Execution of Promises for Individual Results Retrieval - Problem
Parallel Execution of Promises for Individual Results Retrieval

You're given an array of functions that return promises. Your task is to execute all these promises in parallel and collect their results, regardless of whether they succeed or fail.

For each promise:
โ€ข If it resolves: Create an object { status: "fulfilled", value: resolved_value }
โ€ข If it rejects: Create an object { status: "rejected", reason: error_message }

Return a promise that resolves with an array of these result objects, maintaining the same order as the original functions array.

Challenge: Implement this without using the built-in Promise.allSettled() method!

Example:
functions = [() => Promise.resolve(42), () => Promise.reject("Error!")]
Should return: [{status: "fulfilled", value: 42}, {status: "rejected", reason: "Error!"}]

Input & Output

example_1.js โ€” Mixed Success and Failure
$ Input: functions = [ () => Promise.resolve(42), () => Promise.reject("Network Error"), () => Promise.resolve("Hello") ]
โ€บ Output: [ { status: "fulfilled", value: 42 }, { status: "rejected", reason: "Network Error" }, { status: "fulfilled", value: "Hello" } ]
๐Ÿ’ก Note: First and third promises resolve successfully, middle one rejects. All results are captured in order with appropriate status indicators.
example_2.js โ€” All Successful
$ Input: functions = [ () => Promise.resolve(1), () => Promise.resolve(2), () => Promise.resolve(3) ]
โ€บ Output: [ { status: "fulfilled", value: 1 }, { status: "fulfilled", value: 2 }, { status: "fulfilled", value: 3 } ]
๐Ÿ’ก Note: When all promises resolve successfully, each result object has status 'fulfilled' with the corresponding resolved value.
example_3.js โ€” Empty Array Edge Case
$ Input: functions = []
โ€บ Output: []
๐Ÿ’ก Note: Edge case: when given an empty array of functions, return an empty array immediately since there are no promises to execute.

Visualization

Tap to expand
๐Ÿช Restaurant Kitchen Promise Execution๐Ÿ‘จโ€๐ŸณChef 1๐Ÿ Pasta๐Ÿ‘ฉโ€๐ŸณChef 2๐Ÿ”ฅ Burnt๐Ÿ‘จโ€๐ŸณChef 3๐Ÿฅ— Salad๐Ÿ‘ฉโ€๐ŸณChef 4๐Ÿฐ Cake๐Ÿ“‹ Order Completion Trackerโœ… Pasta: { status: "fulfilled", value: "delicious" }โŒ Main: { status: "rejected", reason: "burnt" }โœ… Salad: { status: "fulfilled", value: "fresh" }โœ… Cake: { status: "fulfilled", value: "sweet" }Completed: 4/4 dishes๐ŸŽฏ All dishes completed simultaneously, order ready to serve!
Understanding the Visualization
1
Start All Dishes
All chefs begin cooking their assigned dishes at the same time - this is parallel execution
2
Track Each Dish
As each dish completes (successfully or fails), record the result and increment completion counter
3
Serve Complete Order
When all dishes are done (counter equals total), serve the complete order with status of each dish
Key Takeaway
๐ŸŽฏ Key Insight: Like coordinating multiple chefs in a kitchen, we execute all promises in parallel and track their completion status, ensuring we wait for ALL to finish before returning the complete result set.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(max(t1, t2, ..., tn))

Time is determined by the slowest promise since all run in parallel

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for results array and promise references

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค functions.length โ‰ค 104
  • Each function returns a valid Promise
  • Promise values can be of any type
  • Error messages are strings
  • Must maintain original array order
Asked in
Google 45 Meta 38 Amazon 32 Netflix 28 Microsoft 25
42.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen