1

I am reading up more on Asynchronous Codes - and in this https://developers.google.com/web/fundamentals/primers/async-functions by Google, I realise that that if I add await to every line of code, sometimes it takes longer to process due to the code running in Series rather than Parallel.

Here's the 2 code samples provided on that page.

async function series() {
  await wait(500); // Wait 500ms…
  await wait(500); // …then wait another 500ms.
  return "done!";
}


async function parallel() {
  const wait1 = wait(500); // Start a 500ms timer asynchronously…
  const wait2 = wait(500); // …meaning this timer happens in parallel.
  await wait1; // Wait 500ms for the first timer…
  await wait2; // …by which time this timer has already finished.
  return "done!";
}

Can I understand as both codes look similar, as in they use await on the function wait1 and wait 2. What makes one parallel while the other in series?

the_only_sa
  • 165
  • 1
  • 8
  • https://www.oxfordlearnersdictionaries.com/definition/english/await – Kaiido Dec 22 '17 at 06:40
  • JavaScript is single-threaded, thus there is no such thing as parallel execution of operations - only asynchronous execution. – ethane Dec 22 '17 at 07:12
  • @NoMoreQuestions Yes js is single threaded, but nobody said that what await is awaiting is only ran by js. If you take e.g the fetching of network resources, the tasks of fetching the resources will be made in parallel and both snippets in the question will be really different. – Kaiido Dec 22 '17 at 07:35

2 Answers2

0

The reason why series() could be slower is because you are firing off your first async call waiting for it to return, while that is happening your method is blocked at that point. Once that async call returns your next async call will execute, you will then also block your method and wait for the result of that call.

'Parallel' could be faster because you are firing off both your async calls nigh simultaneously, then you await both those calls thereafter. So in parallel() you are basically starting your async method executions (wait1 and wait2) at the same time.

ethane
  • 2,329
  • 3
  • 22
  • 33
  • Hi @NoMoreQuestions, I still don't fully get it - the second code for `parallel` still have 2 await - how does that make it run in parallel? don't they have to wait for one another to complete? Unless you are saying that the operation runs at const line instead of the await line. – the_only_sa Dec 23 '17 at 17:36
  • Yes, in the second snippet you are starting both operations at `const wait* = wait(500);` That is the only difference between your two snippets (starting both operations without one waiting for the other). Remember, you will still `await` both operations individually meaning that the result of `wait2` will wait for `wait1` even if `wait2` completes first. – ethane Dec 23 '17 at 20:26
-2

As you might know JS is Asynchnorous. Which means when a Function is execueted, JS doesn't wait for it to complete before moving on to the next line of code. This is a source of frustration sometimes.

Having said that...take a look at this function again:

 const wait1 = wait(500); // wait() is executed & JS Moves on i.e. no waiting
  const wait2 = wait(500); //wait() is executed again & JS moves on again.

  await wait1; // wait(500) has been execueting, so we just have wait for it to finish
  await wait2; // 2nd wait(500) also has been excueting so we just have to wait for it to finish.

As a result, these functions have been executing in parallel and will have better performance than the 1st example since the functions in the 1st example execute sequentially. In the 2nd example, they execute in parallel but we just force JS to return the results of the functions in the order we require.

Here is another way of looking at it:

Await Sequentially

Example:

var result1 = await wait1(2);
var result2 = await wait2(result1);
var result3 = await wait3(result2);

Await Asynchnorously

Example:

var results = await Promise.all([wait1(1), wait2(2)]);
smooth_smoothie
  • 1,319
  • 10
  • 27
  • 40
  • https://medium.com/@peterchang_82818/asycn-await-bible-sequential-parallel-and-nest-4d1db7b8b95c – smooth_smoothie Dec 22 '17 at 06:47
  • Hi @smooth_smoothie, Thanks for your response, but I have always been reading that Javascript/ NodeJs is synchronous in nature, unless you do async calls via libraries or setTimeout (or similar functions). So it completes the current stack till completion... but your answer says that JS is asynchronous so I am abit confused here. Anyone can help? :) – the_only_sa Dec 23 '17 at 17:38
  • No you are mistaken. It's actually the other way around. https://stackoverflow.com/a/26804844/373466. Also please upvote if you found my answer helpful. – smooth_smoothie Dec 23 '17 at 19:24