Implement Promise.all

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Promise.all

Approach

Return a new Promise that tracks an array of results and a completion counter. For each input promise, attach a then handler that stores the resolved value at the correct index and increments the counter. Resolve when the counter reaches the total; reject immediately on any failure.

Complexity Analysis

Time
O(n) where n is the number of promises
Space
O(n) for the results array

Pattern

Promise.all Polyfill

Why It Works

Storing results by index rather than push order preserves the original promise ordering, and the counter ensures we wait for all promises regardless of which resolves first.

Updated Feb 2026