Async/Await explained to a five years old child.

This post was inspired by a famous subreddit called “explain like i’am five” where people asks tough questions to be explained in a simple way, like how do we do for a five years old child.

There, you can find interesting questions like: this and this. Beware! you can get addicted to ELI5.

The foundation underneath this is that if you can explain a complex subject in a simple manner is because you really understand that subject.

The Explanation

Imagine two guys, a painter and a art seller, they want to start a business together and the flow of their business is the follow:

  • The seller asks the painter to paint something (a beautiful landscape, an abstract art, a caricature, etc).
  • The painter starts to paint. Some paintings are hard and takes time to finish, others are easy and takes less time.
  • No matter how much time it will consume, the seller waits (doing absolutely nothing) the painter to finish his job.
  • Once the painter finishes his work, the seller try to sell the art work.

After a while, they realize that the seller doesn’t need to wait the painter to finish his work. The seller can try to sell the artwork as soon as the painter starts the painting, now, they start to work in a asynchronous way, the flow of the work becomes the following:

  • The seller asks the painter to paint something and immediately tries to sell this artwork.
  • The painter starts to paint and the seller starts to try to sell the artwork.
  • Once the painter finishes his work, the seller can deliver the artwork to the buyer.

The first flow describes exactly how a computer program work, line-by-line, instruction-by-instruction, the next instruction only can be executed after the previous.

The second flow describes how an async computer program could work, it can work instruction-by-instruction too, but, when something takes more time to finish other things that not depends on the slow instruction can be executed while the slow instruction is executed too.

Show me the code!

I designed a code to show this two work flows in practice. First the obvious one, the synchronous flow:

Nothing new, this flow is very familiar to us and the output of the synchronous method is the following:

And now, the async version of the same method:

The comments of the code are self explanatory, but, look at the line 8, the  myself.PaintAsync(drawingSpecification) is returning an  Task<PaintingResult>, it  means that the painting is in progress and, while it is not finished you can execute other things, like TryToSellTheDrawingToLocalArtGallery(); TryToSellThePaintingAtAmazon(); TryToSellThePaintingAteBay(); .

The output of the async method is the following:

Still confused? It’s because your developer brain are trying to make you think in instructions, but in the async/await world, things can overlaps. There is no better way to understand async/await than put the hands on the code, to save your time, you can download the complete project at my Github. No IDE? No Problem! You could execute this .Net Fiddler.

Note: Async/Await isn’t multithread, although, you can use multithread with async/await but it is subject to another post.