The second example is a normal call synchronous, that is to say, DoFoo()
is running, at a given time transfers the execution flow to Foo()
, internally is practically a goto
, when this one finishes executing another goto
runs back to the next point where the Foo()
(there are some details of how it does this control that is not the case, even for being internal to the processor).
The first example is still two normal functions, but the calling form is quite different, it is asynchronous which allows the execution to be released to perform other things.
There is a complex infrastructure mounted by the C# compiler to control a state machine that controls the execution of methods. Not just a simple flow deviation. And there’s a graph that shows how the flow becomes complex:
Note the black flow as it goes back and forth normally. Red starts running the function but may not end up allowing another stream to run from there, even while that function is waiting for external responses like disk, network, services and database.
So the difference is that the asynchronous method waits for the end of the execution of the function but allows another flow to be executed, while the synchronous method stands there waiting for the end of the processing.
But there is no miracle, somehow it is necessary to have an infrastructure that accesses the external mechanism giving the chance of another flow run. If you are only consuming something you don’t have to worry too much, but if you are writing external access you need to coordinate it. See an example of how is access to read asynchronous file (no . NET Core), is not so trivial.
There has always been the asynchronous form in the . NET, but it was too complicated to consume because you had to write the code to coordinate the flow, now the compiler writes for you.
I will not go into detail about the usefulness and functioning because there are already several answers to this:
Hello Vinicius. If you have patience, give a read in that reply I gave the other question. I tried to create an analogy to explain the queue of requests that the server receives and meets, with and without using the
async/await
. Was an example half-bare, but I think I can get the idea.– Alisson