Working of await in Async await

.net, async-await, c#, c#-4.0, entity-framework

Solution

If I use await, will it wait till the `async` function is executed and then execute the next line.

That depends on the method returns. It returns a `Task[<T>]`, which represents a future value. If the `Task` is already completed, it keeps running synchronously. Otherwise, the remaining portion of the current method is added as a continuation - a delegate callback that is invoked when the asynchronous portion reports completion.

What it does not do, in either case, is block the calling thread while the asynchronous part continues.

And, if I don't use await, will it continue with executing the code below it without waiting for the `async` function to complete.

Yes

...or await is mandatory when it comes to `async` functions?

When invoking them? Not mandatory, but it does make things convenient. Usually, such functions return a `Task[<T>]`, which can also be consumed via different code. You could use `.ContinueWith(...)`, `.Wait`, `.Result`, etc. Or you could just ignore the `Task` completely. The last is a bad idea, because exceptions on asynchronous functions need to go somewhere: very bad things happen if exceptions are not observed.

When writing them, however: if you write an `async` method and don't have an `await` keyword in there, the compiler will present a warning indicating that you're probably doing something wrong. Again, not strictly mandatory, but highly recommended.

Problem

Consider an asynchronous (`*Async` / `async`) function that is called twice, one time with `await` and other without it. If I use await, will it wait until the asynchronous function is executed and then execute the next line? ``` await db.SaveChangesAsync(); //Some code here ``` And, if I don't use `await`, will it continue with executing the code below it without waiting for the asynchronous function to complete? ``` db.SaveChangesAsync(); //Some code here ``` Am, I right over here or await is mandatory when it comes to `async` functions?

Original source