Parallel vs Sequential Promises

PromisesAsync/Await

This question was asked at Intuit interview. Here's the link to the question

You are given two asynchronous functions to implement:

  • A() should resolve to the number 2 after 2 seconds
  • B() should resolve to the number 3 after 3 seconds

Return their sum in two ways:

  • Parallel execution → Total time of execution: 3 seconds
  • Sequential execution → Total time of execution: 5 seconds

Examples

Example 1
Input
// After implementing A, B, sequential and parallel:
(async () => {
  await evaluate(sequential, "SEQUENTIAL");
  await evaluate(parallel, "PARALLEL");
})();
Output
Executing SEQUENTIAL task starts...
Task SEQUENTIAL finished in ~5000 ms with sum: 5
Executing PARALLEL task starts...
Task PARALLEL finished in ~3000 ms with sum: 5
Explanation
In the sequential version, B starts only after A finishes, so total time is roughly 2s + 3s. In the parallel version, A and B run at the same time, so total time is dominated by the slower one (~3s).

Hints

Read the full write-up for Parallel vs Sequential Promises
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it