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 INPUT promise1 resolves to: 2 promise2 resolves to: 5 Input Values: promise1 --> 2 promise2 --> 5 ALGORITHM STEPS 1 Use Promise.all() Pass array of promises 2 Run Concurrently Both promises execute together 3 Await Results Get array [2, 5] 4 Sum Values Return 2 + 5 = 7 async function addTwo( p1, p2) { const [a, b] = await Promise.all( [p1, p2]); return a + b; FINAL RESULT Promise.all resolves: 2 5 Sum Operation: 2 + 5 = 7 OUTPUT 7 OK Key Insight: Promise.all() runs both promises concurrently and returns an array of resolved values. This is more efficient than awaiting promises sequentially. Use destructuring to get individual values. TutorialsPoint - Add Two Promises | Promise.all Concurrent Approach
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