Leet Code — 2723. Add Two Promises

Ben Pereira - Sep 6 '23 - - Dev Community

That’s an easy problem with the description being:

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

Promise in javascript per mdn web docs:

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

A Promise is in one of these states:

pending: initial state, neither fulfilled nor rejected.

fulfilled: meaning that the operation was completed successfully.

rejected: meaning that the operation failed.
The eventual state of a pending promise can either be fulfilled with a value or rejected with a reason (error). When either of these options occur, the associated handlers queued up by a promise’s then method are called. If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.

A promise is said to be settled if it is either fulfilled or rejected, but not pending.

As being said here there is several ways to get this done, a simple one would be just use await which is an operator used to wait for the Promise and execute it, sum and return the value:

var addTwoPromises = async function(promise1, promise2) {
    return (await promise1)+(await promise2);
};
Enter fullscreen mode Exit fullscreen mode

Runtime: 68 ms, faster than 26.87% of JavaScript online submissions for Add Two Promises.
Memory Usage: 42.3 MB, less than 15.01% of JavaScript online submissions for Add Two Promises.

This is not a bad idea. But there is better ways to get it done, that are little bit faster as well.

What if instead of waiting for each we use function that’s incorporated into the Promise itself? then is the one that comes to my mind, so the solution would be:

var addTwoPromises = async function(promise1, promise2) {
    return promise1.then(v1 => promise2.then(v2 => v1+v2));
};
Enter fullscreen mode Exit fullscreen mode

Runtime: 56 ms, faster than 80.88% of JavaScript online submissions for Add Two Promises.
Memory Usage: 42.2 MB, less than 23.23% of JavaScript online submissions for Add Two Promises.

A little bit faster than await, memory usage is the same, but still not generic enough, what if you don’t know later how many promises you might have?

So for multiple promises that might be used later the results for any aggregation functionality Promise all is the one to be used and also is faster:

var addTwoPromises = async function(promise1, promise2) {
    const [value1, value2] = await Promise.all([promise1, promise2]);
    return value1 + value2;
};
Enter fullscreen mode Exit fullscreen mode

Runtime: 55 ms, faster than 84.29% of JavaScript online submissions for Add Two Promises.
Memory Usage: 42.3 MB, less than 23.23% of JavaScript online submissions for Add Two Promises.

Just a bit, nothing too important but this allows you to pass an array and returns an array which could make much easier if you start to have more promises at hand needed to be handled in their own way.


That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .