Magicsheet logo

Add Two Promises

Easy
12.5%
Updated 8/1/2025

Asked by 4 Companies

Topics

Add Two Promises

What is this problem about?

The Add Two Promises coding problem is a modern JavaScript-focused challenge. You are given two promises that eventually resolve to numbers. Your goal is to return a new promise that resolves to the sum of these two numbers only after both original promises have successfully resolved.

Why is this asked in interviews?

This is a popular question for frontend and full-stack roles at Microsoft, Google, and Meta. It tests your understanding of Asynchronous Programming, specifically how to handle multiple concurrent operations. It evaluates whether you know how to use built-in methods to wait for multiple asynchronous tasks rather than nesting callbacks (avoiding "callback hell").

Algorithmic pattern used

The primary pattern here is Asynchronous Concurrency using Promise.all or the async/await syntax. These tools allow you to pause execution until a group of promises settle, which is the most efficient way to coordinate parallel tasks in a non-blocking environment.

Example explanation

Imagine you are fetching the price of two different stocks from two separate API calls:

  • promise1 resolves to 200 after 50ms.
  • promise2 resolves to 150 after 100ms.

Using async/await, your code waits for both to finish. Even though promise1 finishes first, the logic stays "suspended" until promise2 finishes at 100ms. Once both are ready, it adds 200+150200 + 150 and returns 350.

Common mistakes candidates make

  • Sequential Execution: Using await on the first promise before even starting the second. This doubles the total time taken. You should start both, then wait for them.
  • Ignoring Errors: Forgetting that if one promise fails (rejects), the whole operation should be handled gracefully.
  • Incorrect Return Types: Returning the sum directly as a number instead of returning it wrapped in a new Promise.

Interview preparation tip

Get comfortable with Promise.all(). It is the standard tool for this pattern. Also, be prepared to discuss the difference between Promise.all (fails if one fails) and Promise.allSettled (waits for all regardless of success).

Similar Questions