Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an async method without await in a non-async method?

What is the behavior of calling an async method without the await and in a non-async method? I'm asking because I see Visual Studio not displaying any warning on the call to the async method as if it were a perfectly normal thing to do. Does the async method behave as if it were synchronous in such case?

like image 938
whatever Avatar asked Aug 29 '17 15:08

whatever


People also ask

Can I call an async method without await?

The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the Await operator to the result. 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.

What happens if I call an async function without await?

Marking a function as async without using await or returning a value inside it can lead to an unintended promise return and a larger transpiled output. Often the function can be synchronous and the async keyword is there by mistake.

How do you call async function inside non async function?

Just treat async call as promise and attach . then to it: async function wait() { await new Promise(resolve => setTimeout(resolve, 1000)); return 10; } function f() { // shows 10 after 1 second wait().

What happens if you dont use await?

What if you don't use await with async ? The call meant to be Asynchronous becomes Synchronous and would immediately impact the system scalability, as threads are now blocked, even worse for a long running IO operations.


2 Answers

The fact that a method has async as a modifier is an implementation detail of the method. It just means you can write asynchronous code more easily. It doesn't change how the method is called at all.

Typically an async method returns Task or Task<T>. It can return void, but that's usually only used for event handlers, as otherwise the caller can't tell when the method has completed. In C# 7 it can also return a custom task type.

Now how the caller uses that return value is up to them. Suppose a method returns Task<int> - a synchronous method might call that method then ignore the returned task completely ("fire and forget"), or attach a continuation with ContinueWith, or pass it to an async method that will await it. None of that changes how the async method itself behaves.

like image 143
Jon Skeet Avatar answered Nov 15 '22 00:11

Jon Skeet


It's not uncommon for async methods to be called in this fire-and-forget fashion (and yes, the usual advice regarding not using/being extra careful with async void also fully applies in this case).

Calling an async method from a method which does not have the async modifier does not result in the async method executing synchronously.

Instead, the async method will execute up to the first await on an awaitable having IsCompleted == false (i.e. first awaited instance of Task which does not complete synchronously). At that point the calling (outer) method will resume execution, and the rest of the async method will be scheduled to run at a later point.

like image 37
Kirill Shlenskiy Avatar answered Nov 15 '22 00:11

Kirill Shlenskiy