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 numberThe key idea in #2723 Add Two Promises is to work with JavaScript's asynchronous programming model. Each input is a Promise that eventually resolves to a numeric value. Instead of handling them sequentially, an efficient approach is to resolve both promises concurrently and then combine their results.
A common strategy is to use Promise-based concurrency utilities such as Promise.all. This allows both promises to execute in parallel while waiting until both are fulfilled. Once the resolved values are available, their sum can be calculated and returned as the final result.
Another clean approach is using async/await, which provides a more readable syntax for awaiting promise resolution. By awaiting both promises and adding their results, you can return a new promise that resolves to the computed sum.
Because only a constant number of operations and variables are involved, the algorithm runs in O(1) time and uses O(1) auxiliary space, excluding the time taken by the promises themselves to resolve.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Concurrent Promise Resolution (Promise.all / async-await) | O(1) | O(1) |
NeetCode
Time Complexity: O(1) since promise creation and resolution are constant time operations.
Space Complexity: O(1) as we only store a fixed number of numbers.
1function addTwoPromises(promise1, promise2) {
2 return Promise.all([promise1, promise2]).then(values => {
3 return values[0] + values[1];
4 });
5}
6// Example usage:
7let promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20));
8let promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60));
9addTwoPromises(promise1, promise2).then(console.log); // Output: 7In this code, we first call `Promise.all` with an array of the two input promises promise1 and promise2. This returns a promise that resolves when both input promises resolve. We then use the `then` method to specify that when the promise resolves, it should sum the two resolved values and return the result.
Time Complexity: O(1) as each step essentially involves constant-time resolution.
Space Complexity: O(1) since we're working with a fixed number of variables.
1function addTwoPromises(promise1, promise2) {
2 return promise1.then(value1 =>This approach utilizes the Promise.all() method, which takes an array of promises and returns a new promise that resolves when all of the promises in the array have resolved.
In this method, you pass both promise1 and promise2 to Promise.all(). It waits for both promises to resolve, then combines their resolved values using array destructuring to sum them up within the then() method.
Time Complexity: O(1), as all operations inside the promises are essentially constant time operations.
Space Complexity: O(1), space usage is constant regardless of input sizes.
1function addTwoPromises(promise1, promise2) {
2 return Promise
This approach employs async/await, a cleaner syntax for handling promises in JavaScript. By using an async function, you can await the resolution of both promise1 and promise2 to get their values before summing them up.
Time Complexity: O(1), as the time taken by promise resolution does not scale with input data.
Space Complexity: O(1), as the function only stores a few values regardless of input.
1async function addTwoPromises(promise1, promise2) {
2 const value1 =
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, similar questions appear in interviews to test understanding of asynchronous programming in JavaScript. Interviewers often check whether candidates know how to manage multiple promises efficiently using Promise.all or async/await.
This problem does not require any complex data structures. It primarily uses JavaScript Promise objects and simple arithmetic to combine the resolved results.
The optimal approach is to resolve both promises concurrently using Promise.all or async/await. Once both promises resolve, simply add their values and return the result as a new promise. This keeps the implementation concise and efficient.
The problem mainly relies on JavaScript's Promise API for asynchronous programming. Utilities like Promise.all or the async/await syntax make it easy to wait for multiple promises and combine their resolved values.
This code captures each promise's resolution value one after the other by chaining the then method, first resolving promise1, storing its value, and then resolving promise2, accumulating the sum and returning it.
The function addTwoPromises takes two promises and returns a new promise that resolves to the sum of the values resolved by the input promises. The Promise.all method is used to wait for both promises to resolve, and the resolved values are extracted as an array within the then method.
addTwoPromises is defined as an async function, meaning it returns a Promise and its execution can be paused using await. By awaiting both promise1 and promise2, the function captures their resolved values into value1 and value2. These are summed and the result is returned, thus promise chaining is implicit in every call.