1

According to google docs explanation of Async the following function named 'parallel' should execute in parallel, and the function named 'series' should execute sequentially.
Async Functions Explanation on Google Developers

Parallel should take 2 seconds to complete.
Series should take 4 seconds.
However, they both complete in 2 seconds.

Why doesn't series() take 4 seconds total? I am expecting the First timer to complete after 2 seconds, then the second timer to complete another 2 seconds later?

async function series() {
    function timer(time) { setTimeout(function(){ console.log(`Hello ${time}`) }, time); }

    await timer(2000);  // Wait 2 seconds…
    await timer(2000);  // …should wait another 2 seconds
}
series()  // should take 4 seconds to complete, but only takes 2 seconds

Why does this work in parallel but series does not work in series?

async function parallel() {
    function timer(time) { setTimeout(function(){ console.log(`Hello ${time}`) }, time); }

    let one = timer(2000);  // Start a 2 second timer asynchronously…
    let two = timer(2000);  // …meaning this timer happens in parallel. 
    await one;  // Wait 2 seconds for the first timer
    await two;  // …by which time this time finishes around same time
}
parallel()  // completes in 2 seconds
LoveYouFyi
  • 51
  • 2
  • 10
  • thats because your function returns the value immediately its not returning result after given time – warl0ck Aug 15 '19 at 18:39

5 Answers5

2

The await operator is used to wait for a Promise. If you make your timer function return a promise that resolves when the timeout callback is called then it should work as you expect.

You can read more here: Promise await

1

setTimeout does not return a promise that could be awaited. So it does not wait for the setTimeout to finish. Hence the setTimeout functions are added to the queue and executed immediately in 2 seconds.

If you add promise the second timer will execute after 4 seconds

Chetan Naik
  • 623
  • 1
  • 12
1

In both of your examples, you should be returning promise to check the parallelism and serial-ism of the code snippet.

As in your current example, await only waits till the function is executed, which in your case it is, as setTimeout returns immediately so to understand the example, use Promise to see which returns the value in future.

function timer(time) { 
    return new Promise((resolve, reject) => setTimeout(() => resolve(Math.random()), time), null);
 }
async function series() {
    await timer(2000);  // Wait 2 seconds…
    await timer(2000);  // …should wait another 2 seconds
}
series()  // should take 4 seconds to complete, but only takes 2 seconds

In case of parallel execution

function timer(time) { 
    return new Promise((resolve, reject) => setTimeout(() => resolve(Date.now()), time), null);
 }
async function parallel() {
    let d1 = timer(2000);
    let d2 = timer(2000);
    // here both the functions are invoked and after invoking them we are waiting
    await d1;
    await d2;
}
parallel()
warl0ck
  • 2,804
  • 2
  • 24
  • 46
0

You need to return Promise and resolve it later

function delay(t, v) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, v), t) }); }

Here is explanation: using setTimeout on promise chain

Dominik Matis
  • 1,719
  • 5
  • 14
0

Because in your case you execute setTimeout which gets performed immediately, then next setTimeout.

Then both timers get executed in 2 sec.

Use Promise as in example and will get expected result

Alex Vovchuk
  • 1,698
  • 3
  • 11
  • 32