Home > Enterprise >  Do I always need to use async/await?
Do I always need to use async/await?

Time:12-14

I wanted to ask you about async/await. Namely, why does it always need to be used? (all my friends say so)

Example 1.

public async Task Boo()
    {
        await WriteInfoIntoFile("file.txt");

        some other logic...
    }

I have a Boo method, inside which I write something to files and then execute some logic. Asynchrony is used here so that the stream does not stop while the information is being written to the file. Everything is logical.

Example 2.

public async Task Bar()
    {
        var n = await GetNAsync(nId);
        _uow.NRepository.Remove(n);
        await _uow.CompleteAsync();
    }

But for the second example, I have a question. Why here asynchronously get the entity, if without its presence it will still be impossible to work further?

CodePudding user response:

why does it always need to be used?

It shouldn't always be used. Ideally (and especially for new code), it should be used for most I/O-based operations.

Why here asynchronously get the entity, if without its presence it will still be impossible to work further?

Asynchronous code is all about freeing up the calling thread. This brings two kinds of benefits, depending on where the code is running.

  1. If the calling thread is a UI thread inside a GUI application, then asynchrony frees up the UI thread to handle user input. In other words, the application is more responsive.
  2. If the calling thread is a server-side thread, e.g., an ASP.NET request thread, then asynchrony frees up that thread to handle other user requests. In other words, the server is able to scale further.

CodePudding user response:

Depending on the context, you might or might not get some benefit. In case you call the second function from a desktop application, it allows the UI to stay responsive while the async code is being executed.

CodePudding user response:

Not sure what you mean by

without its presence it will still be impossible to work further

regarding example 2. As far as I can tell this code gets an entity by id from its repository asynchronously, removes it, then completes the transaction on its Unit of Work. Do you mean why it does not simply remove the entry by id? That would certainly be an improvement, but would still leave you with an asynchronous method as CompleteAsync is obviously asynchronous?

As to your general question, I don't think there is a general concensus to always use async/await.

CodePudding user response:

In your second example there with the async/await keywords you are getting the value of the n variable asynchronously. This might be necessary because the GetNAsync method is likely performing some time-consuming operation, such as querying a database or perhaps you might be calling a webservice downstream, that could block the main thread of execution. By calling the method asynchronously, the rest of the code in the Bar method can continue to run while the query is being performed in the background.

But if in the GetNAsync you are just calling another method locally that is doing some basic CPU bound task then the async is pointless in my view. Aync works well when you are sure you need to wait such as network calls or I/O bound calls that will definitely add latency to your stack.

  • Related