Watch 10 video solutions for Add Two Promises, a easy level problem. This walkthrough by Learn With Chirag has 3,516 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
promise1 and promise2, return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.
Example 1:
Input: promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)), promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60)) Output: 7 Explanation: The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.
Example 2:
Input: promise1 = new Promise(resolve => setTimeout(() => resolve(10), 50)), promise2 = new Promise(resolve => setTimeout(() => resolve(-12), 30)) Output: -2 Explanation: The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.
Constraints:
promise1 and promise2 are promises that resolve with a numberProblem Overview: You receive two JavaScript promises, promise1 and promise2, each resolving to a number. The task is to return a new promise that resolves to the sum of their resolved values. The main challenge is coordinating asynchronous resolution correctly.
Approach 1: Chaining Promises (Sequential Resolution) (Time: O(1), Space: O(1))
This approach resolves the first promise and then resolves the second inside the .then() callback. After both values are available, return their sum. The logic uses promise chaining to control execution order. While straightforward, it waits for the first promise before starting to process the second, which can introduce unnecessary delay if both promises could resolve independently. This pattern is common when learning promises but is not the most efficient when tasks are independent.
Approach 2: Using Promise.all (Parallel Resolution) (Time: O(1), Space: O(1))
Promise.all() runs both promises concurrently and resolves once both are complete. It returns an array of resolved values in the same order as the input promises. You can destructure the results and return their sum. This approach avoids sequential waiting and is the most idiomatic way to combine multiple independent promises in JavaScript. Since both promises resolve simultaneously, total wait time equals the slower of the two operations.
Approach 3: Promise.all with Explicit Resolution Logic (Time: O(1), Space: O(1))
This variation still uses Promise.all(), but explicitly returns a new promise that resolves after computing the sum. Internally, it waits for both promises, then performs a simple arithmetic addition. The extra wrapping can be useful if additional asynchronous processing or validation is required before resolving the final result. Functionally it behaves the same as the standard Promise.all() approach but demonstrates how to build composed promise workflows.
Approach 4: Using async/await (Time: O(1), Space: O(1))
An async function allows promises to be written with synchronous-style code. Use await Promise.all([promise1, promise2]) to resolve both values, then return their sum. This approach keeps asynchronous logic easy to read and reduces nested callbacks. Many modern JavaScript codebases prefer async/await because it integrates cleanly with error handling and async control flow in async programming.
Recommended for interviews: Using Promise.all() is the expected solution. It resolves both promises concurrently and keeps the implementation concise. Chaining promises demonstrates understanding of asynchronous execution, but parallel resolution shows stronger familiarity with real-world JavaScript patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Chaining Promises | O(1) | O(1) | When demonstrating basic promise chaining or sequential async execution |
| Promise.all (Parallel) | O(1) | O(1) | Best choice when promises are independent and can resolve concurrently |
| Promise.all with Wrapper Promise | O(1) | O(1) | Useful when adding extra async processing before resolving the final result |
| Async/Await with Promise.all | O(1) | O(1) | Preferred in modern JavaScript for readability and structured async control flow |