Parallel Execution of Promises for Individual Results Retrieval - Problem
Parallel Execution of Promises for Individual Results Retrieval
You're given an array of
For each promise:
โข If it resolves: Create an object
โข If it rejects: Create an object
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
Example:
Should return:
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
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
โ Linear Growth
Space Complexity
O(n)
Space for results array and promise references
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code