Problem : Add Two Promises | #2723 | LeetCode
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.
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.
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.
/**
* @param {Promise} promise1
* @param {Promise} promise2
* @return {Promise}
*/
var addTwoPromises = async function (promise1, promise2) {
const [result1, result2] = await Promise.all([promise1, promise2]);
return Promise.resolve(result1 + result2);
};
/**
* addTwoPromises(Promise.resolve(2), Promise.resolve(2))
* .then(console.log); // 4
*/
Let's break down the code step by step:
1. `addTwoPromises` is a function that takes two promises, `promise1` and `promise2`, as arguments.
2. Inside the `addTwoPromises` function:
- `await Promise.all([promise1, promise2]);` - This line uses `Promise.all` to wait for both `promise1` and `promise2` to resolve. `Promise.all` takes an array of promises and returns a new promise that resolves with an array of their resolved values. In this case, `result1` will hold the resolved value of `promise1`, and `result2` will hold the resolved value of `promise2`.
- `return Promise.resolve(result1 + result2);` - After both promises have resolved, this line calculates the sum of `result1` and `result2`. Then, it returns a new promise using `Promise.resolve` that resolves with the sum as its value.
3. The `addTwoPromises` function is then called with two promises:
addTwoPromises(Promise.resolve(2), Promise.resolve(2))
- `Promise.resolve(2)` creates a promise that immediately resolves with the value `2`.
- `addTwoPromises` is called with two such promises.
4. `.then(console.log)` - This part of the code attaches a `.then` callback to the result of `addTwoPromises`. When the promise returned by `addTwoPromises` resolves (which will happen after both `Promise.resolve(2)` promises have resolved and their values have been added together), the `.then` callback is executed with the result as an argument.
5. `console.log` is used to output the result, so the sum of the two resolved values (`2 + 2`) is printed to the console, resulting in `4` being displayed.
In summary, this code defines a function `addTwoPromises` that takes two promises, waits for them to resolve using `Promise.all`, calculates their sum, and returns a new promise that resolves with the sum. When you call `addTwoPromises` with two promises that resolve to `2`, it returns a promise that eventually resolves to `4`, and `console.log` is used to print that value to the console.