Should I use async if I return the task and do not expect anything

In the asynchronous method, where the code is awaitnothing, is there a reason that someone will mark it async, wait for the task, and then return?

Besides the potential need for it, what are the negative consequences of this?

In this example, suppose it 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);
}
+4
source share
2 answers

No, you shouldn't just add asyncto a method without await- there is even a compiler warning for it.

await , .

- , .

, - , ConfigureAwait(false), . await , , await.

, await , - , async , , , .

+3

Source: https://habr.com/ru/post/1666678/


All Articles