Implement Promise.finally

Easy
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.finally

Approach

Implement then() with two handlers: on fulfill, run the callback via Promise.resolve then return the original value; on reject, run the callback then re-throw. This ensures the callback always executes while the original result or error passes through unchanged.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Promise Wrapper

Why It Works

Wrapping the callback in Promise.resolve handles both sync and async callbacks, while returning the original value or re-throwing preserves the promise chain semantics.

Updated Feb 2026