In an async method where the code is not awaiting anything, is there a reason why someone would mark it async, await the task, and then return?
Besides the potential un-necessity of it, what are the negative ramifications of doing so?
For this example, please assume that QueryAsync<int> returns Task<int>.
private static async Task<int> InsertRecord_AsyncKeyword(SqlConnection openConnection)
{
int autoIncrementedReferralId =
await openConnection.QueryAsync<int>(@"
INSERT INTO...
SELECT CAST(SCOPE_IDENTITY() AS int)"
);
return autoIncrementedReferralId;
}
private static Task<int> InsertRecord_NoAsyncKeyword(SqlConnection openConnection)
{
Task<int> task =
openConnection.QueryAsync<int>(@"
INSERT INTO...
SELECT CAST(SCOPE_IDENTITY() AS int)"
);
return task;
}
// Top level method
using (SqlConnection connection = await DbConnectionFactory.GetOpenConsumerAppSqlConnectionAsync())
{
int result1 = await InsertRecord_NoAsyncKeyword(connection);
int result2 = await InsertRecord_AsyncKeyword(connection);
}
asyncand not both are the same from the perspective of caller. Both are method that returnTask.asynckeyword just enable you to useawaitinside the method, not the caller.awaithas no impact on starting Task (as synchronous portion of the code runs the same in both cases).