Skip to content Skip to sidebar Skip to footer

How Is Async/await Working In Serial And Parallel?

I have two async functions. Both of them are waiting for two 3 seconds function calls. But the second one is faster than the first. I think the faster one is running in parallel an

Solution 1:

Frxstream already has an excellent answer. To build on that, and provide some perspective:

The first thing to recognize is that Promises are created "hot" - that is, by the time you have a promise object, it is already "in progress".

The second important concept is that await is like an "asynchronous wait" - that is, it pauses the execution of the function until that promise completes.

So, the serial function calls sleep, gets a promise back, and then (asynchronously) waits for that promise to complete. After that promise completes 3 seconds later, serial again calls sleep, gets a promise back, and then (asynchronously) waits for that promise to complete. After that promise completes 3 seconds later, serial completes.

The parallel function calls sleep and stores its promise in a, and then calls sleep and stores its promise in b, and then (asynchronously) waits for a to complete. After a completes 3 seconds later, parallel (asynchronously) waits for b to complete. After b completes practically immediately, parallel completes.

Solution 2:

It's clearer if you write your serial function like this:

async function serial() {
  var a = sleep(); //await a;         // await sleep();var b = sleep(); //await b;         // await sleep();
}

async function parallel() {
  var a = sleep();
  var b = sleep();
  await a;
  await b;
}

Here you can clearly see that in the serial function, the second sleep() is only called after the first sleep() has completed, while in parallel it's called immediately, before the first has completed, and then it waits for both to complete. So while they may look functionally identical, they are subtly different.

Solution 3:

Because thesleep()function is a synchronous function, it just return a asynchronous promise, eg:

functionsleep (time) {
    returnnewPromise((resolve) =>setTimeout(resolve, time));
}

In parallel(), the two sleep() synchronously init two promise,they wait to be resolved meanwhile, it's going to be about 3s.

However, in serial(), the two await sleep() means that the second sleep() promise must wait for the first sleep() to be resolved, so it's going to be about 6s.

Solution 4:

Serial Run: Run the functions one after another

// 🚫 It’s slow! and use only for serial(one after another) runasyncfunctiondoThings() {
    const thing1 = awaitasyncThing1(); // waits until resolved/rejectedconsole.log(thing1);

    const thing2 = awaitasyncThing2(); // starts only after asyncThing1() has finished.console.log(thing2);
}

doThings();

Parallel Run: Run the functions parallel

// ✅ async code is run in parallel!asyncfunctiondoThings() {
    const p1 = asyncThing1(); // runs parallelconst p2 = asyncThing2(); // runs parallel// Method 1: Prefer => Promise.all()const [resultThing1, resultThing2] = awaitPromise.all([p1, p2]); // waits for all // the promises get resolved or fails fast (If one of the promises supplied to it // rejects, then the entire thing rejects).// Method 2: Not-Preferredconst resultThing1 = await p1;
    const resultThing2 = await p2;

    console.log(resultThing1); // reaches here only after both the p1 & p2 have completed.console.log(resultThing2);
}

doThings();

Post a Comment for "How Is Async/await Working In Serial And Parallel?"