Parallel Execution of Promises for Individual Results Retrieval - Problem

Given an array of functions, return a promise. The functions array contains functions that return promises.

Each function promise can be resolved or rejected:

  • If the promise is resolved: { status: "fulfilled", value: resolved_value }
  • If the promise is rejected: { status: "rejected", reason: rejection_reason }

The returned promise should resolve with an array of these objects. Each object in the array should correspond to the promises in the original functions array, maintaining the same order.

Note: Try to implement it without using the built-in method Promise.allSettled().

Input & Output

Example 1 — Mixed Results
$ Input: functions = [() => Promise.resolve(42), () => Promise.reject("Error")]
Output: [{"status":"fulfilled","value":42},{"status":"rejected","reason":"Error"}]
💡 Note: First promise resolves with 42, second rejects with "Error". Both results are captured in the array maintaining order.
Example 2 — 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: All three promises resolve successfully, each result has status "fulfilled" with corresponding values.
Example 3 — All Failed
$ Input: functions = [() => Promise.reject("fail1"), () => Promise.reject("fail2")]
Output: [{"status":"rejected","reason":"fail1"},{"status":"rejected","reason":"fail2"}]
💡 Note: Both promises reject with different reasons, both captured as rejected status objects.

Constraints

  • 1 ≤ functions.length ≤ 10
  • functions[i] is a function that returns a Promise
  • Each Promise can resolve or reject with any value

Visualization

Tap to expand
Parallel Promise Execution OverviewInput Functions() => Promise.resolve(42)() => Promise.reject("Error")() => Promise.resolve("OK")ParallelExecutionAll promisesrun simultaneouslywith error handlingResult Array{status:"fulfilled",value:42}{status:"rejected",reason:"Error"}{status:"fulfilled",value:"OK"}⚡ All promises execute simultaneouslySuccess and failure cases both capturedOriginal order maintained in result arrayPerformance BenefitTime = max(promise_durations) instead of sum
Understanding the Visualization
1
Input
Array of functions returning promises
2
Execute
Run all promises in parallel
3
Collect
Gather results maintaining order
Key Takeaway
🎯 Key Insight: Execute all promises concurrently and handle both success/failure cases uniformly
Asked in
Meta 25 Google 20 Microsoft 18 Amazon 15
28.4K Views
Medium Frequency
~15 min Avg. Time
890 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