1

I have a method like this:

private AsyncResourceGetter _getter;
public Task<string> DoSomeWork()
{
   return _getter.GetAsync();
}

I'm thinking of changing it to:

public async Task<string> DoSomeWork()
{
   return await _getter.GetAsync();
}

What's the difference? what's the right approach here?

barakcaf
  • 1,294
  • 2
  • 16
  • 27
  • 4
    The second one creates a state machine inside DoSomeWork() – ProgrammingLlama Aug 31 '21 at 07:02
  • 5
    See here: https://blog.stephencleary.com/2016/12/eliding-async-await.html – Tomer Aug 31 '21 at 07:03
  • 6
    A source on these topics I'd always recommend is Stephen C : [Eliding async await](https://blog.stephencleary.com/2016/12/eliding-async-await.html) – Fildor Aug 31 '21 at 07:03
  • 1
    In this particular case, eliding async await is technically fine. Nothing would go wrong here. That said, I still hold to Stephen Cleary’s recommendation of not doing so. – Alexander Høst Sep 04 '21 at 13:42

0 Answers0