I try to not create redundant Task
objects in my code and I write Task
returning functions instead of async Task
functions where it is possible.
When it is necessary to save value returned by an async
function, I am forced to make the function return async Task
and call function with await
.
Example:
async Task SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
}
What I wrote instead:
Task SomeWorkAsync()
{
var task = AnotherWorkAsync();
someGlobalVariable = task.Result;
return task;
}
But I am afraid that it will block calling thread as synchronous code does.
await SomeWorkAsync(); //main thread block
Is there another way to rewrite the code in example without wrapping a whole function with new Task
as async
keyword does?