Explorer
JavaScript

Write a function `promiseAllSettled(promises)` that waits for all promises and returns their status and value/reason.

Problem Statement

<p>Write a function <code>promiseAllSettled(promises)</code> that waits for all promises and returns their status and value/reason.</p>

Examples

Input: promiseAllSettled([Promise.resolve(1), Promise.reject("err")])

Output: [{ status: "fulfilled", value: 1 }, { status: "rejected", reason: "err" }]

Explanation: Always resolves with status records for each promise.

Complexity

Time Complexity: O(N)

Space Complexity: O(N)

Hints

šŸ’” Hint 1: Unlike Promise.all, this never rejects early. šŸ’” Hint 2: For each promise, record {status:"fulfilled", value} or {status:"rejected", reason}. šŸ’” Hint 3: Use a counter. When all promises have settled, resolve with the results array.

Editorial & Approach

Problem Overview & Intuition

To solve Promise.allSettled Implementation, we consider the execution characteristics of JavaScript engines. Write a function `promiseAllSettled(promises)` that waits for all promises and returns their status and value/reason. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (unlike promise.all, this never rejects early).
  3. Implement Logic: Handle state and transformations efficiently (for each promise, record {status:"fulfilled", value} or {status:"rejected", reason}).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function promiseAllSettled(promises) {
  return new Promise(resolve => {
    if (promises.length === 0) return resolve([]);
    const results = [];
    let count = 0;
    promises.forEach((p, i) => {
      Promise.resolve(p)
        .then(value => { results[i] = {status:"fulfilled", value}; })
        .catch(reason => { results[i] = {status:"rejected", reason}; })
        .finally(() => { count++; if (count === promises.length) resolve(results); });
    });
  });
}

Complexity Analysis

Time Complexity O(N) parallel promise registration
Space Complexity O(N) output buffer array

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

Promise.allSettled Implementation

Medium

Write a function promiseAllSettled(promises) that waits for all promises and returns their status and value/reason.

Example Scenarios
1Example 1
Input: promiseAllSettled([Promise.resolve(1), Promise.reject("err")])
Output: [{ status: "fulfilled", value: 1 }, { status: "rejected", reason: "err" }]
Explanation:

Always resolves with status records for each promise.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[{"status":"fulfilled","value":1},{"status":"rejected","reason":"error"},{"status":"fulfilled","value":3}]