Is there an easy way in .net to wait for an async function to fetch its returned result?
I wrote code like this but maybe there is a standard solution?
T WaitForResult<T>(Func<Task<T>> async)
{
Task<T> task = async.Invoke();
task.Wait();
return task.Result;
}
e.g.
async Task<string> f()
{
...
}
void g()
{
string str = WaitForResult( () => f() );
}
If we use await instead that, the g
function would have to be marked as async. How to avoid this?