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.
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").
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.
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 and returns 350.
await on the first promise before even starting the second. This doubles the total time taken. You should start both, then wait for them.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).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sum of Digits of String After Convert | Easy | Solve | |
| Apply Transform Over Each Element in Array | Easy | Solve | |
| Array Prototype Last | Easy | Solve | |
| Binary Tree Postorder Traversal | Easy | Solve | |
| Check If N and Its Double Exist | Easy | Solve |