Async / await transformation and pitfalls


    /// <summary>
    /// please read first http://blog.stephencleary.com/2012/02/async-and-await.html
    /// </summary>

Let’s say you want to transform this code to async / await

public bool TwoTask()
        {
            var file= WriteToFile();
            var console= ReadConsole();
            return file & console;
        }

The first version is


public async Task<bool> TwoTask()
        {
            var file=await WriteToFile();
            var console = await ReadConsole();
            return file & console;
        }

Why is not good ? Because it executes in serial the file, and then the console code

The good version is


public async Task<bool> TwoTask()
        {
            var file=WriteToFile();
            var console = ReadConsole();
            await Task.WhenAll(file, console);
            return file.Result & console.Result;
        }

You can see in action at https://youtu.be/02oO0YaTEyM