Add Two Promises - Problem

Given two promises 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.

Input & Output

Example 1 — Basic Addition
$ Input: promise1 resolves to 2, promise2 resolves to 5
Output: 7
💡 Note: Both promises resolve with numbers, and 2 + 5 = 7
Example 2 — Negative Numbers
$ Input: promise1 resolves to -4, promise2 resolves to 9
Output: 5
💡 Note: Adding negative and positive: -4 + 9 = 5
Example 3 — Both Negative
$ Input: promise1 resolves to -3, promise2 resolves to -7
Output: -10
💡 Note: Both negative numbers: -3 + (-7) = -10

Constraints

  • promise1 and promise2 will always resolve with numbers
  • Numbers can be positive, negative, or zero
  • The promises will not reject

Visualization

Tap to expand
Add Two Promises: Combine Promise ValuesPromise 1resolves to5Promise 2resolves to3Addition Operation5 + 3= 8ResultPromise resolves to8Two promises resolve to numbers, return their sum as a new promise
Understanding the Visualization
1
Input
Two promises that resolve to numbers
2
Process
Await both promises and get their values
3
Output
Return sum of the two resolved values
Key Takeaway
🎯 Key Insight: Use Promise.all for concurrent execution to optimize performance
Asked in
Google 25 Microsoft 20 Amazon 18 Facebook 15
12.5K Views
Medium Frequency
~10 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen