Home What is the purpose of "return await" in C#?
Reply: 0

What is the purpose of "return await" in C#?

user1005
1#
user1005 Published in May 23, 2018, 10:42 pm

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 a 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 should 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?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.466445 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO