Difficulty: Easy | Acceptance: 91.70% | Paid: No Topics: N/A
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.
- Examples
- Constraints
- Approach 1: Using Promise.all
- Approach 2: Using async/await
- Approach 3: Chaining Promises
Examples
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 the 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 the value of 10 + (-12) = -2.
Constraints
promise1 and promise2 are promises that resolve with a number
Approach 1: Using Promise.all
Intuition Promise.all allows us to wait for multiple promises to resolve simultaneously and returns an array of their results, which we can then sum.
Steps
- Use Promise.all to wait for both promises to resolve
- Extract the values from the resolved array
- Return the sum of the two values
python
import asyncio
class Solution:
async def addTwoPromises(self, promise1: asyncio.Future, promise2: asyncio.Future) -> asyncio.Future:
result1 = await promise1
result2 = await promise2
return result1 + result2Complexity
- Time: O(1) - Just waiting for promises and adding two numbers
- Space: O(1) - Only storing the two resolved values
- Notes: Most efficient for concurrent promise resolution
Approach 2: Using async/await
Intuition Using async/await syntax, we can sequentially wait for each promise to resolve and then return their sum in a clean, readable manner.
Steps
- Await the first promise to get its resolved value
- Await the second promise to get its resolved value
- Return the sum of both values
python
import asyncio
class Solution:
async def addTwoPromises(self, promise1: asyncio.Future, promise2: asyncio.Future) -> asyncio.Future:
val1 = await promise1
val2 = await promise2
return val1 + val2Complexity
- Time: O(1) - Sequential waiting and addition
- Space: O(1) - Only storing two values
- Notes: More readable but waits sequentially rather than concurrently
Approach 3: Chaining Promises
Intuition Chain promises using .then() to handle the results sequentially, where we first resolve one promise, then use its value to resolve the second and sum them.
Steps
- Use .then() on the first promise to get its resolved value
- Inside the callback, use .then() on the second promise
- Return the sum of both values in the final callback
python
import asyncio
class Solution:
async def addTwoPromises(self, promise1: asyncio.Future, promise2: asyncio.Future) -> asyncio.Future:
async def sum_values():
val1 = await promise1
val2 = await promise2
return val1 + val2
return asyncio.create_task(sum_values())Complexity
- Time: O(1) - Sequential promise resolution and addition
- Space: O(1) - Only storing intermediate values
- Notes: Traditional promise chaining approach, more verbose but demonstrates understanding of promise mechanics