2723. Add Two Promises
On this page, we explore LeetCode Problem 2723: Add Two Promises.
This problem requires you to work with JavaScript's Promise API and return a new promise that resolves with the sum of two resolved values.
📄 Problem Statement
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.
💡 Example
Input:
addTwoPromises(Promise.resolve(2), Promise.resolve(5)).then(console.log);
Output:
7
🧠 Explanation
We can use Promise.all() to wait for both promises to resolve. Once they do, we destructure the values and return their sum. This allows the function to run both promises concurrently and efficiently.
✅ Your Solution
/**
* @param {Promise} promise1
* @param {Promise} promise2
* @return {Promise}
*/
var addTwoPromises = async function(promise1, promise2) {
const [val1, val2] = await Promise.all([promise1, promise2]);
return val1 + val2;
};