Introduce to Async/Await by making breakfast

Energy boy
3 min readJun 17, 2022

--

This is my first blog on medium. The reason why I want to start writing blog is I hope I can summarize the concept that used to annoy me but get familiar now. I hope I can help the people who need. There is a big gap between school and industry. Some important concepts will not teach at school. My aim is to talk those concepts in the easy way.

The first concept I want to talk about is Async/Await. So…What is it really do for? What is the different if our code base on “asynchronous” and “synchronous”?

Let me tell you a story. I am preparing my lovely breakfast. I want a coffee and a toast with egg and bacon. That us break down what should we do for each item. First, I will make my coffee with coffee maker (3 mins). Second, I will fry the egg with pan (1 min). Third, fry the bacon with pan (1 min). Fourth, toast my white bread with toaster (2 min). Finally, combine egg, bacon and toaster (1 min).

Start with the synchronous process! The synchronous means that the next task will not start when current task is doing or waiting for a result.

The overall time for making breakfast is:

3 + 1 +1 +2 +1 = 8 mins

It is not bad actually! But we can do it much better!

Go back to the line we have seen “The synchronous means that the next task will not start when current task is doing or waiting for a result.”

Wait! You might ask hey that is normal that I cannot do anything while task one is running, I only have one person to prepare the breakfast! Yes, you are right! The job like frying egg, bacon and combine all together, those jobs are done by you, it means that you have to take care for all of the time. But think about one thing…while you are making the coffee, is that really “You” make the coffee? Obviously not! The coffee maker makes the coffee. The only thing you do is just waiting for the coffee maker to finish its job. Same to toasting bread, you do not really do thing, the toaster do.

This comes to the concept of “asynchronous”. The smart people will say…” Hey! I can fry the egg and bacon while coffee is making! The coffee maker will make sound when it finished. I just need to take it off from the coffee maker while it done!” Brilliant thought! You already know the key concept of asynchronous. You might notice that when the bacon was fried, we still have time to put the bread into toaster!

Just look at the new asynchronous process that we just talked.

We only take 5 mins to finish our breakfast! (In 3 min all we need is just take off the coffee from coffee maker, it did not cost much time.)

Please look at the job at 5 min. This is the most interesting thing in asynchronous process. As we told before, toaster toast the bread not you, so we can do another job while the bread is toasting. But! We cannot combine things together if the bread was not toasted! So, before we step into combining, we need to “await” for the toasting process!

There is many programming language support Async/Await. In next article, I will implement it with python and C#.

Conclusion:

It is very important to know that when we talk about asynchronous, we are not talking about multithreading (In the story above, there is only one person), we are talk about how we can improve our overall time by arrange the task. Before arranging the task, we need to know what kind of job we should take care of our self. Or just waiting for a result that will response from another process.

It is also important to know what the task need to be done before another task start. Honestly, I think this is the most interesting part in Async/Await.

--

--

Energy boy

Start my Software Engineer career on March 2019. I love to deep dive into complicated concept and share it.