David Fowler (ASP.NET Architect) has an excellent guidance.
I copy here the relevant part:
Prefer async
/await
over directly returning Task
There are benefits to using the async/await keyword instead of directly returning the Task:
- Asynchronous and synchronous exceptions are normalized to always be asynchronous.
- The code is easier to modify (consider adding a
using
, for example).
- Diagnostics of asynchronous methods are easier (debugging hangs etc).
- Exceptions thrown will be automatically wrapped in the returned Task instead of surprising the caller with an actual exception.
❌ BAD This example directly returns the Task to the caller.
public Task<int> DoSomethingAsync()
{
return CallDependencyAsync();
}
✅ GOOD This examples uses async/await instead of directly returning the Task.
public async Task<int> DoSomethingAsync()
{
return await CallDependencyAsync();
}
NOTE: There are performance considerations when using an async state machine over directly returning the Task
. It's always faster to directly return the Task
since it does less work but you end up changing the behavior and potentially losing some of the benefits of the async state machine.