Run functions in parallel and in sequence in JavaScript

This interview question was asked in Intuit’s SDE2 frontend interview, where the user was asked to run functions in parallel and in sequence in JavaScript.

The problem statement reads as:

  • Write two functions:
  • A() returns 2 after 2 seconds
  • B() returns 3 after 3 seconds
  • Return their sum in two ways:
  • Parallel execution → Total time: 3 seconds
  • Sequential execution → Total time: 5 seconds

This problem evaluates where you understanding of asynchronous programming in JavaScript.

To solve this problem we will first create a function wait() that will take the number as input and return the same number after a delay of number * 1000 milliseconds.

The wait() function will basically return a promise that will resolve with the number as a value after the specified amount of time.

const wait = (num) => {
  return new Promise(resolve => setTimeout(resolve, num * 1000, num));
}

This will be a reusable function that we will use for both function A() and B().

const A = async () => {
  return wait(2);
}

const B = async () => {
  return wait(3);
}

Now, to run them in sequence that is one after another we will have to start the next execution only when previous one ends. To do this we will use the await keyword before the function so that it executes in sequence.

/** 
 * These will be run in sequence, because we call
 * a function and immediately wait for each result.
 */
const series = async () => {
  // This will be executed first
  const result1 = await A();

  // This will be executed after
  const result2 = await B();

  return result1 + result2;
}

To run them parallely, we will execute them simultaneously, but get there value later.

/** 
 * These will be run in parallel where we call the functions first,
 * then wait for the result later
 */
const parallel = async () => {
  // execution starts parallely
  const task1 = A();
  const task2 = B();
  
  const result1 = await task1;
  const result2 = await task2;

  return result1 + result2;
}

In the sequence, the second function execution starts only when the first function is completed. In the parallel, both are executed simultaneously but we get the value later.

This is where it becomes tricky and many of us gets confused, if we have not understood it thoroughly.

Now, we will execute these functions parallel() and series() and evaluate there performance. For that I have created a helper function evaluate() that will measure the performance of the function.

const evaluate = async (fn, label) => {
  const startTime = performance.now();
  console.log(`Executing ${label} task starts...`);
  let result = await fn();
  const endTime = performance.now();
  console.log(`Task ${label} finished in ${ Number.parseInt(endTime - startTime) } milliseconds with sum:`, result);
}

Testcase

Input:
evaluate(series, 'sequential');
evaluate(parallel, 'parallel');

Output:
"Task sequential starting..."

"Task parallel starting..."

"Task parallel finished in 3029 milliseconds with," 5

"Task sequential finished in 5011 milliseconds with," 5