DEV Community

Cover image for Asynchronous Programming
MansaMusa55
MansaMusa55

Posted on

Asynchronous Programming

As much as we all love computers they can be very difficult to deal with. Especially as Developers. One of the most crucial aspects of building an application is making sure that it runs in a timely manner. Even the most complex algorithm is no good if it takes 5 hours to run. This problem typically arises when the code is written in a way where it executes block-by-block. This is where Asynchronous Programming can save the day.

Asynchronous Programming is basically a form of parallel programming that allows a block of code to execute on its own separate from the main application thread. Once the block of asynchronous code is complete it will notify the main thread. Now let’s put this logic into an example all of us can understand.

The next time you cook breakfast try to make everything individually. For example if you wanted to make scrambled eggs, sausage, toast and a cup of juice you would have to make the eggs first but you can’t work on other components of your breakfast. So now that the eggs are complete you can finally begin frying the sausages. Let’s say that sausages take 10 minutes. That means that within those 10 minutes you can’t simply put the bread in the toaster to get your toast. As you can see this can end up being very time consuming.

With Asynchronous Programming the workflow of executing code or making breakfast saves a lot of time and headache for the user. Now that we are operating asynchronously let’s take a look at the process.

  1. Warm the pan for eggs.
  2. Warm another pan for sausages.
  3. Depending on which pan warms up first, put the respected item in the pan.
  4. Cook eggs.
  5. Flip sausage.
  6. Put bread in a toaster.
  7. Flip sausage.
  8. Put jelly on toast.
  9. Put eggs, sausage, and toast on a plate.
  10. Pour juice.

Making breakfast asynchronously will save you about half the time and trouble compared to performing in a synchronous manner. In my next article I will explain how to implement this concept into lines of code while using the Await and Async keywords so keep on the lookout for part 2!

Top comments (0)