I'm looking at the example method
public async Task MyMethodAsync()
{
Task<int> longRunningTask = LongRunningOperationAsync();
// independent work which doesn't need the result of LongRunningOperationAsync can be done here
//and now we call await on the task
int result = await longRunningTask;
//use the result
Console.WriteLine(result);
}
from How and When to use `async` and `await` and I have a question about how it works.
So let's say I add some "independent work" where it says I can.
public async Task MyMethodAsync()
{
Task<int> longRunningTask = LongRunningOperationAsync();
int millionthPrimeNumber = GetMillionthPrimeNumber();
int result = await longRunningTask;
//use the result
Console.WriteLine(result % millionthPrimeNumber);
}
Getting the millionth prime number is independent work and so I expect that it is the type of thing that would be sandwiched between the Task<int> longRunningTask = LongRunningOperationAsync();
and int result = await longRunningTask;
However, what I'm curious about is whether I could just write
public async Task MyMethodAsync()
{
int result = await LongRunningOperationAsync();
int millionthPrimeNumber = GetMillionthPrimeNumber();
//use the result
Console.WriteLine(result % millionthPrimeNumber);
}
and would that be equivalent or not? Or does the Task<int> longRunningTask = LongRunningOperationAsync();
and int result = await longRunningTask;
actually serve as flags so that the independent work that can be done is whatever is between them?