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();
}
2
  • 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. Commented Sep 14, 2018 at 16:56
  • The correct way is to make all your calling methods async as this sort of thing can lead to blocking issues. Commented Sep 14, 2018 at 16:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.