Explorer
JavaScript

Write a function `promiseAll(promises)` that works like Promise.all — resolves with an array of results when all promises resolve, or rejects with the first rejection.

Problem Statement

<p>Write a function <code>promiseAll(promises)</code> that works like Promise.all — resolves with an array of results when all promises resolve, or rejects with the first rejection.</p>

Examples

Input: promiseAll([Promise.resolve(1), Promise.resolve(2)])

Output: [1, 2]

Explanation: Resolves when all promises fulfill.

Complexity

Time Complexity: O(N)

Space Complexity: O(N)

Hints

💡 Hint 1: Return a new Promise. Track results in an array. 💡 Hint 2: Use a counter to know when all promises have resolved. 💡 Hint 3: results[i] = value ensures order is preserved. Increment counter and resolve when counter equals promises.length.

Editorial & Approach

Problem Overview & Intuition

To solve Promise.all Implementation, we consider the execution characteristics of JavaScript engines. Write a function `promiseAll(promises)` that works like Promise.all — resolves with an array of results when all promises resolve, or rejects with the first rejection. 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 (return a new promise. track results in an array).
  3. Implement Logic: Handle state and transformations efficiently (use a counter to know when all promises have resolved).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function promiseAll(promises) {
  return new Promise((resolve, reject) => {
    if (promises.length === 0) return resolve([]);
    const results = [];
    let count = 0;
    promises.forEach((p, i) => {
      Promise.resolve(p).then(val => {
        results[i] = val;
        count++;
        if (count === promises.length) resolve(results);
      }).catch(reject);
    });
  });
}

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.all Implementation

Medium

Write a function promiseAll(promises) that works like Promise.all — resolves with an array of results when all promises resolve, or rejects with the first rejection.

Example Scenarios
1Example 1
Input: promiseAll([Promise.resolve(1), Promise.resolve(2)])
Output: [1, 2]
Explanation:

Resolves when all promises fulfill.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[1,2,3]