Sobes.tech
Junior — Senior

Summing the results of two promises

livecode

Task condition

Task: implement an asynchronous function addTwoPromises that takes two Promises, each resolving to a number, and returns their sum.


 const addTwoPromises = async (promise1, promise2) => { };

console.log()

/* */

* addTwoPromises( Promise.resolve(2), Promise.resolve(2 ) )
* .then( console.log ); // 4

Examples:

  • Input:
const p1 = new Promise(resolve => setTimeout(() => resolve(2), 20));
const p2 = new Promise(resolve => setTimeout(() => resolve(5), 60));

Output: 7 Explanation: the first promise returns 2, the second — 5, their sum is 7.

  • Input:
const p1 = new Promise(resolve => setTimeout(() => resolve(10), 50));
const p2 = new Promise(resolve => setTimeout(() => resolve(-12), 30));

Output: -2 Explanation: 10 + (-12) = -2.