Let me quote the code snippet and its relevant explanation from Async and Await article written by Stephen Cleary as follows.
public async Task DoSomethingAsync()
{
// In the Real World, we would actually do something...
// For this example, we're just going to (asynchronously) wait 100ms.
await Task.Delay(100);
}
The beginning of an async method is executed just like any other method. That is, it runs synchronously until it hits an “await” (or throws an exception).
The “await” keyword is where things can get asynchronous. Await is like a unary operator: it takes a single argument, an awaitable (an “awaitable” is an asynchronous operation). Await examines that awaitable to see if it has already completed; if the awaitable has already completed, then the method just continues running (synchronously, just like a regular method).
If “await” sees that the awaitable has not completed, then it acts asynchronously. It tells the awaitable to run the remainder of the method when it completes, and then returns from the async method.
To accommodate more general case, let me define the code snippet as follows.
public async Task DoSomethingAsync()
{
AAA();
await BBBAsync;
CCC();
}
where BBBAsync
is the only "awaitable" operation or method.
For the sake of simplicity, let's assume we execute DoSomethingAsync
from within the main app thread. So in my understanding,
AAA()
will always be executed synchronously.BBBAsync()
will always be executed asynchronously no matter howDoSomethingAsync()
is invoked.CCC()
may be executed either asynchronously or synchronously depending on the result of theawait
operator determines.
It seems to me, the await
operator always find the awaitable has not been completed yet because await
always comes before BBBAsync()
starts.
Question
When does await examine the awaitable?