Why use Async/await all the way down
Solution 1:
A better slogan is async
all the way up. Because you start with an asynchronous operation and make its caller asynchronous and then the next caller etc.
You should use async-await
when you have an inherently asynchronous operation (usually I/O but not necessarily) and you don't want to waste a thread idly waiting for the operation to complete. Choosing an async
operation instead of a synchronous one doesn't speed up the operation. It will take the same amount of time (or even more). It just enables that thread to continue executing some other CPU bound work instead of wasting resources.
But to be able to await
that operation the method needs to be an async
one and the caller needs to await
it and so forth and so forth.
So async
all the way up enables you to actually make an asynchronous call and release any threads. If it isn't async
all the way then some thread is being blocked.
So, this:
async Task FooAsync()
{
await Func1();
// do other stuff
}
async Task Func1()
{
await Func2();
// do other stuff
}
async Task Func2()
{
await tcpClient.SendAsync();
// do other stuff
}
Is better than this:
void Foo()
{
Func1();
// do other stuff
}
void Func1()
{
Func2().Wait(); // Synchronously blocking a thread.
// do other stuff
}
async Task Func2()
{
await tcpClient.SendAsync();
// do other stuff
}
Solution 2:
The major benefit is the fact that awaiting an asynchronous method returns the worker thread to the pool to be used in other calls (web requests to your .NET MVC web app, for example). The asynchronous work is done on an IO completion thread. When the awaited method finishes, another worker thread will collect the results and resume execution. This prevents worker thread pool exhaustion and allows your application to handle more load (CPU, memory, and network throughput depending).
As for "await all the way down", that seems like an issue to me. Typically await
is associated to an external resource (DB call, HTTP request, etc.) that your application must wait on. If you await code that doesn't have external IO dependencies, you're creating overhead that isn't needed. It's possible to have multiple awaits
in an async
method chain, but awaiting some code that itself calls await
but has no other external IO dependency is not good and will just add callback/compiler overhead.
Solution 3:
Usually, the reasoning behind async
/await
goes the other way around:
For whatever reason, you decide that Func2
would be easier to write using await
. So you simplify the method by adding the desired await
s, meaning you also have to change the method signature (it will now include the async
keyword and a return type of Task
or Task<T>
).
Because the return type has changed, you can no longer call Func2
like you used to (var result = Func2();
), so you're now required to change the calling method, Func1
. The easiest way to adapt Func1
will often be to make it async
too, and await Func2()
.
Then, for the same reason (changed signature), you will need to change all calls to Func1
, and so on until you get to some kind of entry point (either a UI event handler, or your Main
method, or something else).
So you don't start making the "outermost" method async
and follow through to the "inner" (called) methods; you usually make things async
while going in the opposite direction (from the "innermost" methods back to the calling ones). This other answer calls this idea "async all the way up".
Solution 4:
"This async method lacks 'await' operators and will run synchronously" is what happens if you don't await an async method. To harness the benefits of an async method you must await it, turning the caller into an async method which can be awaited etc etc.
From the flow diagram you see that the async method returns a Task (a promise of work to be completed in the future) and yields control to it's caller. The caller can then get on with work not dependent on this result in the meantime. Clearly this needs to bubble up the call stack to find all of this gainful work that can be done without the result (In a UI app this work would include unblocking the UI, which is why it's async all the way up to the the event handler in the below example).
From my initial misreading. So you've found some async code that you need to call, is it worth the async await pattern spreading up your code:
I've heard a lot that the main problem with async-await is it's a much too easy syntax for what it actually does. Program flow gets complicated. I really like async-await, but unfortunately in most the async code I've seen it isn't worth it and is just needlessly ruining my call-stack.
A good thing to keep in mind is the 50ms rule.
"This is the rule that Microsoft followed with the WinRT APIs; anything taking less than 50ms is considered “fast” and close enough to “immediate” that they do not require an asynchronous API."
This is being used in the context of encouraging async-await. However, I think it equally should be applied to tell developers using async-await for essentially immediate functionality to cut it out.