LeetCode Problem Workspace
Add Two Promises
The problem 'Add Two Promises' involves adding the resolved values of two promises and returning the result.
0
Topics
2
Code langs
0
Related
Practice Focus
Easy · Add Two Promises core interview pattern
Answer-first summary
The problem 'Add Two Promises' involves adding the resolved values of two promises and returning the result.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Add Two Promises core interview pattern
The 'Add Two Promises' problem requires combining two promises that resolve with numbers. The task is to return a promise that resolves to the sum of the two values once both promises are resolved. Be mindful of the asynchronous nature of the task, as the order of resolution can vary.
Problem Statement
In this problem, you are given two promises that resolve with numeric values. Your task is to return a new promise that resolves with the sum of the values resolved by the two input promises. The final result should be the sum of the two numbers, and it should resolve only when both promises have completed.
The key challenge here is to manage the asynchronous nature of promises. You must ensure that both promises resolve before performing the addition. The order in which the promises resolve is not guaranteed, so you will need to handle this situation accordingly.
Examples
Example 1
Input: promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)), promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
Output: 7
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
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 number
Solution Approach
Using Promise.all()
One approach to solving this problem is using Promise.all(). This method takes an array of promises and resolves once all promises have completed. You can use Promise.all() to wait for both input promises to resolve and then sum their values.
Using async/await
Another approach is to use async/await. This can help make your code more readable and avoid callback chains. You can wait for both promises to resolve and then add their values together in a clean and concise manner.
Using .then()
You can also use .then() chaining to handle the resolution of the promises. Once both promises are resolved, you can add their values inside a .then() method to return the sum. While this method can work, it may require additional handling to ensure both promises are resolved before performing the addition.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time and space complexity of this problem depend on the specific approach used. With Promise.all(), the time complexity is O(n), where n is the number of promises being resolved. The space complexity is O(1) as no extra space is used beyond the promises themselves. If async/await or .then() chaining is used, the complexities are similar, but there may be slight variations in space usage depending on how the promises are handled internally.
What Interviewers Usually Probe
- Check if the candidate demonstrates an understanding of promise resolution order.
- Look for efficient handling of asynchronous behavior and edge cases.
- Evaluate the candidate's ability to implement solutions using modern JavaScript features like async/await.
Common Pitfalls or Variants
Common pitfalls
- Not properly handling the case where one promise resolves before the other, potentially causing issues with the addition.
- Failing to consider edge cases where the promises resolve to non-numeric values.
- Using nested .then() chains unnecessarily, making the code harder to maintain and read.
Follow-up variants
- Modify the problem to add more than two promises.
- Change the problem to handle promises that resolve to objects instead of numbers.
- Have the promises resolve after different time intervals and measure the time it takes for the sum to be returned.
FAQ
What is the 'Add Two Promises' problem about?
The 'Add Two Promises' problem asks you to return a promise that resolves with the sum of two other promises' resolved values.
How can I handle promises resolving in different orders?
Use Promise.all(), async/await, or .then() chaining to ensure both promises resolve before performing the addition.
What is the best way to approach this problem?
The best approach depends on your familiarity with async/await or Promise methods. Promise.all() is simple for multiple promises, while async/await can lead to cleaner code.
Can I use .then() to solve this problem?
Yes, .then() can be used to handle promise resolution, but it's often less readable compared to async/await or Promise.all().
What should I consider about performance in this problem?
Performance isn't a major concern for this problem, but you should focus on the correct asynchronous handling of promises. The time complexity is typically O(n), where n is the number of promises.
Solution
Solution 1
#### TypeScript
var addTwoPromises = async function (promise1, promise2) {
return (await promise1) + (await promise2);
};