Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async/Await Execution Difference

I'm trying to get a good grasp of async/await and I want to clear some of the confusion. Can someone please explain what would be the difference in terms of execution for the following:

// version 1
public Task Copy(string source, string destination) {
    return Task.Run(() => File.Copy(source, destination));
}

public async Task Test() {
    await Copy("test", "test2");    
    // do other stuff
}

And:

// version 2
public async Task Copy(string source, string destination) {
    await Task.Run(() => File.Copy(source, destination));
}

public async Task Test() {
    await Copy("test", "test2");
    // ...
}

Are they resulting in the same code and why would I write one over the other ?

like image 290
Dimitar Dimitrov Avatar asked Apr 20 '14 10:04

Dimitar Dimitrov


People also ask

What is difference between async and await?

The async keyword is used to define an asynchronous function, which returns a AsyncFunction object. The await keyword is used to pause async function execution until a Promise is fulfilled, that is resolved or rejected, and to resume execution of the async function after fulfillment.

Does async await stop execution?

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment.

What is the difference between async await and callback?

The await keyword is used in an async function to ensure that all promises returned in the async function are synchronized, ie. they wait for each other. Await eliminates the use of callbacks in .

Is async await better than then catch?

In my view, unless a library or legacy codebase forces you to use then/catch , the better choice for readability and maintainability is async/await . To demonstrate that, we'll use both syntaxes to solve the same problem.


1 Answers

First of let me start with the point that both codes are not same.

Your version1 code will create only one "State machine" as it contains await in Test method only.

Your version2 code will create two "state machines" for Copy and Test method which adds some overhead.

Why do we use async methods? Simple just to make our code readable, elegant while dealing with "Asynchronous Tasks". It makes our code better avoiding callbacks and continuations etc.

Let's break down what Copy method is doing and we answer the question whether we really need it to be async?

Copy method simply delegates the call to Task.Run which returns a task that eventually reaches completion on File.Copy's completion. So the intent is clear here we need a task which notifies File.Copy completion. This method does all what you need, no need for it to be async to work as expected.

So, When do you need async?

You need async when you need to execute some code upon earlier task's completion(Continuation).

Example:

public async Task Test() 
{
    await Copy("test", "test2");
    DoPostCopied(whatever);
    await DoPostCopied2();//Etc
}

You can verify this difference between async and non async method just by decompiling both versions. It is too long and won't be readable so I skipped posting it here.

Conclusion: Use async only when required. In this case version1 is better and you should prefer it over version2.

like image 148
Sriram Sakthivel Avatar answered Sep 20 '22 22:09

Sriram Sakthivel