LeetCode 2723. Add Two Promises Solution in JavaScript & TypeScript | Explanation + Code

CoderIndeed
0
2723. Add Two Promises

Description

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 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 number

Solutions

Solution 1

TypeScriptJavaScript
async function addTwoPromises( promise1: Promise<number>, promise2: Promise<number>, ): Promise<number> { return (await promise1) + (await promise2); } /** * addTwoPromises(Promise.resolve(2), Promise.resolve(2)) * .then(console.log); // 4 */(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !