How Async and Await works

I’ve been taught about it in the following fashion, I found it to be quite a clear and concise explanation:

//this is pseudocode
async Method()
{
    code;
    code;
    await something; 
    moreCode;
}

When Method is invoked, it executes its contents (code; lines) up to await something;. At that point, something; is fired and the method ends like a return; was there.

something; does what it needs to and then returns.

When something; returns, execution gets back to Method and proceeds from the await onward, executing moreCode;

In a even more schematic fashion, here’s what happens:

  1. Method is invoked
  2. code; is executed
  3. something; is executed, flow goes back to the point where Method was invoked
  4. execution goes on with what comes after the Method invocation
  5. when something; returns, flow returns inside Method
  6. moreCode; is executed and Method itself ends (yes, there could be something else await-ing on it too, and so on and so forth)

Leave a Comment