Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I await an async method without an async modifier in this parent method?

Tags:

I have a method that I want to await but I don't want to cause a domino effect thinking anything can call this calling method and await it. For example, I have this method:

public bool Save(string data) {    int rowsAffected = await UpdateDataAsync(data);    return rowsAffected > 0; } 

I'm calling:

public Task<int> UpdateDataAsync() {   return Task.Run(() =>   {     return Data.Update(); //return an integer of rowsAffected   } } 

This won't work because I have to put "async" in the method signature for Save() and then I can't return bool I have to make it Task<bool> but I don't want anyone awaiting the Save() method.

Is there a way I can suspend the code execution like await or somehow await this code without the async modifier?

like image 586
Neal Avatar asked Jun 01 '13 21:06

Neal


People also ask

Can we use await without async?

You can use the await keyword on its own (outside of an async function) within a JavaScript module. This means modules, with child modules that use await , wait for the child module to execute before they themselves run.

Can we use await without async in C#?

Every now and then you'll find yourself in a synchronous method (i.e. one that doesn't return a Task or Task<T> ) but you want to call an async method. However, without marking the method as async you can't use the await keyword.

What happens when you call async method without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Can you use await without async Python?

The await syntax can be only used inside async functions, and that's not generally a problem because we simply need to declare the function as async by prepending the async keyword to its definition.


Video Answer


1 Answers

How can I await an async method without an async modifier in this parent method?

That's kind of like asking "how can I write an application using C# but without taking a dependency on any kind of .NET runtime?"

Short answer: don't do that.

Really, what you're doing here is taking a naturally-synchronous method (Update), making it appear asynchronous by running it on a thread pool thread (UpdateDataAsync), and then you're wanting to block on it to make the asynchronous method appear synchronous (Save). Serious red flags.

I recommend you carefully study Stephen Toub's famous pair of blog posts should I expose asynchronous wrappers for my synchronous methods and should I expose synchronous wrappers for my asynchronous methods. The answer to both questions is "no", though Stephen Toub explains several options to do it if you really have to.

That "really have to" should be reserved for the application level. I assume these methods (Update, UpdateDataAsync, and Save) are in different layers of the application (e.g., data / data service / view model). The data / data service layers should not be doing synchronous/asynchronous conversions. The view model (application-specific) level is the only one that has an excuse to do that kind of conversion -- and it should only do so as a last resort.

like image 67
Stephen Cleary Avatar answered Sep 28 '22 12:09

Stephen Cleary