Add Two Promises - Problem

Welcome to the world of asynchronous JavaScript! In this problem, you'll work with Promises - one of JavaScript's most powerful features for handling asynchronous operations.

You are given two promises: promise1 and promise2. Both promises will eventually resolve with numeric values. Your task is to create and return a new promise that resolves with the sum of these two numbers.

Goal: Create a promise that waits for both input promises to resolve, then returns their sum.

Input: Two Promise objects that resolve to numbers

Output: A Promise that resolves to the sum of the two input numbers

This problem teaches fundamental concepts of promise handling, async/await syntax, and error management in modern JavaScript applications.

Input & Output

example_1.js โ€” Basic Promise Addition
$ Input: promise1 = Promise.resolve(2), promise2 = Promise.resolve(5)
โ€บ Output: Promise that resolves to 7
๐Ÿ’ก Note: Both promises resolve immediately with their respective values (2 and 5), and our function returns a promise that resolves to their sum (2 + 5 = 7).
example_2.js โ€” Delayed Promises
$ Input: promise1 = new Promise(resolve => setTimeout(() => resolve(10), 1000)), promise2 = new Promise(resolve => setTimeout(() => resolve(15), 500))
โ€บ Output: Promise that resolves to 25 after 1 second
๐Ÿ’ก Note: The first promise resolves after 1 second with value 10, the second resolves after 0.5 seconds with value 15. The result promise waits for both and resolves to 25.
example_3.js โ€” Negative Numbers
$ Input: promise1 = Promise.resolve(-3), promise2 = Promise.resolve(8)
โ€บ Output: Promise that resolves to 5
๐Ÿ’ก Note: Even with negative numbers, the addition works correctly: -3 + 8 = 5. This demonstrates the function handles all numeric values.

Constraints

  • Both promise1 and promise2 will resolve with number values
  • The promises may resolve immediately or after some delay
  • Numbers can be positive, negative, or zero
  • Numbers are within JavaScript's safe integer range (-253 to 253)
  • The returned value must be a Promise that resolves to the sum

Visualization

Tap to expand
Restaurant APromise 1Restaurant BPromise 2Kitchen(Promise.all)Combining...Final MealSum ResultOrder AOrder BDelivered!๐Ÿ•๐ŸŸโœ“
Understanding the Visualization
1
Place Two Orders
Start both promises - they begin executing independently
2
Wait for Delivery
Use Promise.all() or await to wait for both results
3
Combine the Meal
Add the two numbers together
4
Serve the Result
Return a new Promise containing the sum
Key Takeaway
๐ŸŽฏ Key Insight: Promises allow asynchronous operations to be composed elegantly. Use Promise.all() for concurrent execution when operations are independent, or async/await for more readable sequential or concurrent code.
Asked in
Google 28 Meta 22 Amazon 18 Microsoft 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 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