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 numberWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're trying to add the results of two things that will eventually give us numbers. To do it the simplest way, we wait for both of them to finish and then simply add the numbers they give us together.
Here's how the algorithm would work step-by-step:
import asyncio
async def add_two_promises(promise1, promise2):
# We need to wait for the first promise to resolve to a number
first_promise_value = await promise1
# Then, we need to wait for the second promise
second_promise_value = await promise2
# Finally, we can safely add the values together.
sum_of_promises = first_promise_value + second_promise_value
return sum_of_promisesWe need to add the results of two asynchronous operations that complete at different times. The key is to wait for both operations to finish before calculating and returning their sum. We achieve this by leveraging the promise mechanism to handle the asynchronous nature of the operations and return the result as a promise.
Here's how the algorithm would work step-by-step:
import asyncio
async def add_two_promises(promise_one, promise_two):
# Ensure both promises complete before proceeding.
result_one = await promise_one
result_two = await promise_two
# Calculate the sum of the resolved values.
sum_of_results = result_one + result_two
# Return a new promise that resolves to the sum.
return sum_of_results| Case | How to Handle |
|---|---|
| One or both promises reject | Reject the resulting promise immediately if either of the input promises reject. |
| Non-numeric values returned by promises | Check the types of the resolved values and reject the result if they are not numbers. |
| Promises that never resolve or reject (infinite pending) | Implement a timeout mechanism to reject the promise if it takes too long to resolve. |
| Large numeric values that could lead to overflow | Javascript numbers are double-precision floating point, so very large numbers will lose precision, which the solution should handle gracefully, although exact precision is not guaranteed with extremely large numbers. |
| Promises resolving with NaN or Infinity | Explicitly check for NaN and Infinity in the resolved values and reject the promise if encountered. |
| Promises that resolve with negative numbers | Negative number results are valid based on the problem statement, and should be handled normally through standard addition. |
| Promises that resolve to 0 | The solution works correctly with zero as it will return the sum normally. |
| Extremely long resolution times of promises | Consider a timeout to avoid indefinite waiting; reject the promise after a reasonable duration to prevent resource exhaustion. |