1

Suppose I had an async method like

public async Task<bool> GetBoolAsync() 
{
    // Do some stuff
    return await GetMyBoolFromSomeFunction(somestuff);
}

and I wanted a synchronous version, this compiles just fine, but is it the correct way?

public bool GetBool() 
{
    return GetBoolAsync().Result;
}

or if there were no return value,

public async Task DoSomethingAsync() 
{
    // Do some stuff
    await DoSomethingFromSomeFunction(somestuff);
}

public void DoSomething() 
{
    DoSomethingAsync().Wait();
}
Rafael
  • 7,605
  • 13
  • 31
  • 46
jo phul
  • 639
  • 1
  • 9
  • 29
  • The correct way is to go async all the way. Sometimes you have to fake it for a while so you can use `Task.FromResult(...)` to keep the method "asynchronous". Otherwise `ConfigureAwait` and `GetAwaiter().GetResult()` might work. It depends. – FCin Sep 14 '18 at 16:56
  • The correct way is to make all your calling methods async as this sort of thing can lead to blocking issues. – juharr Sep 14 '18 at 16:56

0 Answers0