I'm trying to understand if there's an optimisation in skipping async/await in certain situations or whether this could lead to a pitfall. Code below is made up example but it'll help to simplify for my question and understanding.
Say I have the following code:
public async Task<string> CreateRandomString()
{
var myTitleTask = GetTitle();
var randomString = CreateRandomString();
var myTitle = await myTitleTask;
return myTitle + randomString;
}
private async Task<string> GetTitle()
{
return await GetTitleFromWebServiceAsync();
}
It's possible to remove the async
/await
from the GetTitle()
method like this:
public async Task<string> CreateRandomString()
{
var myTitleTask = GetTitle();
var randomString = CreateRandomString();
var myTitle = await myTitleTask;
return myTitle + randomString;
}
private Task<string> GetTitle()
{
return GetTitleFromWebServiceAsync();
}
Does this optimise anything because we are delaying as long as possible to await the Task
from GetTitle()
and thus doing other work until we await or does this cause any problems?
In the second example I thought this was more optimise and better approach but just want to make sure I'm not falling into any pitfall. Thoughts or comments on this please?