Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using async without await

I'd like to make a function async, so I simply add async like this:

public async static void something(){ } 

You can see that its return-type is void. I just want this function to be called asynchronously without blocking, since return is void so no await is needed.

But Visual Studio 2012 just cannot compile this, it says that I miss await?

Could you please advise a sample that makes a function async without using await.

like image 473
Eric Yin Avatar asked Aug 18 '12 06:08

Eric Yin


People also ask

Can I use async without await?

In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously. Code after each await expression can be thought of as existing in a .then callback.

What happens if you call async 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 we use async without await C++?

Rule: no-async-without-awaitFunctions marked async must contain an await or return statement.

Can you use async without await Python?

You don't have to await it. Period. @HossamAl-Dokkani Listen, there's no way to run an async function on a not-running loop. You have to start the loop.


1 Answers

I think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

Also, you should try to avoid using async void methods, they make handling exceptions difficult.

like image 58
svick Avatar answered Oct 06 '22 06:10

svick