Is there any scenario where writing method like this:
public async Task<SomeResult> DoSomethingAsync()
{
// Some synchronous code might or might not be here... //
return await DoAnotherThingAsync();
}
instead of this:
public Task<SomeResult> DoSomethingAsync()
{
// Some synchronous code might or might not be here... //
return DoAnotherThingAsync();
}
would make sense?
Why use return await
construct when you can directly return Task<T>
from the inner DoAnotherThingAsync()
invocation?
I see code with return await
in so many places, I think I might have missed something. But as far as I understand, not using async/await keywords in this case and directly returning the Task would be functionally equivalent. Why add additional overhead of additional await
layer?
DoAnotherThingAsync
is written with async/await, then the async/await here is redundant and there's no different in exception propagation.DoAnotherThingAsync
already implements the state machine that captures any exceptions thrown and embeds them in the returned Task. The only time you'd want to add async/await here is if the called methodDoAnotherThingAsync
does not use async/await and may throw exceptions. In that case, an exception could be thrown before the Task is returned, changing the required error handling approach. That's really the only difference. – Caracaraballo