After reading this otherwise very in-depth blog post, I still cannot understand how the Task<TResult>
is really constructed before it is returned from the async method.
I get how the compiled method initializes and runs the state machine for the first time. Then if the first await is not already completed, it registers a continuation for it to the MoveNext()
method that is currently running and then returns from it to the compiled method.
At this point the compiled method returns builder.Task<TResult>
.
I get a bit of the magic behind it by browsing the source code, i.e. it being an instance of the Task derived AsyncStateMachineBox<TStateMachine>
but I do not understand what this task represents when it is first constructed.
Is it the continuation we just registered with the first thing to be awiated? But then what if there are other awaits that need their own continuation tasks?