DEV Community

Muhammad Khazin
Muhammad Khazin

Posted on

What is Asynchronous?

Before we talk about asynchronous, let’s first understand synchronous. Imagine you're cooking a simple meal, and you're following these steps:

🍳 Dish 1: Boiled egg and toast

Method: Synchronous

Dish: Boiled egg and toast

  1. Boil water.
  2. Wait until it boils.
  3. Put egg in. Wait 10 minutes.
  4. Take egg out.
  5. Toast bread.
  6. Serve

It’s straightforward, linear, and simple. You’re doing one thing at a time, step-by-step. For this kind of meal, it works fine.

But what happens when you’re making a more complex dish like the one with many ingredients, longer prep, and multiple processes?

Let's look at below example to compare another dish cooked in both synchronous and asynchronous way.

🍝 Dish 2: The Complex Meal

Method: Synchronous

Dish: Pasta with garlic bread

👨‍🍳 "I can only do one thing at a time. I must wait for each task to finish before moving on."

  1. Boil water
  2. Cook pasta after water is boiled
  3. Chop vegetables after pasta is cooked.
  4. Roast vegetables
  5. Prepare sauce ingredients after roasting is done.
  6. Cook sauce
  7. Slice and toast garlic bread after sauce is done.
  8. Pasta is done, sauce is ready, veggies are roasted → plate everything hot at the same time.

⏱️ Total time = Long
You end up wasting a lot of time just waiting, even though there are tasks that could have been done in parallel.

🍝 Dish 2: The Complex Meal

Method: Asynchronous

Dish: Pasta with garlic bread

👨‍🍳 "While something is cooking or waiting, I’ll prep or cook something else"

  1. Start boiling the water.
  2. While waiting for it to boil, chop the vegetables.
  3. Put the vegetables in the oven to roast
  4. While they roast, start mixing the sauce ingredients.
  5. Once the water boils, cook the pasta while the sauce simmers.
  6. Toast the garlic bread while the sauce finishes and veggies roast.
  7. Pasta is done, sauce is ready, veggies are roasted → plate everything hot at the same time.

⏱️ Total time = Much shorter

Style Who Description
Synchronous A beginner cooks (maybe you?) Cooks one thing at a time, step-by-step. Simple but slow.
Asynchronous Your mom in the kitchen 💪 Juggles multiple things at once. Efficient and experienced.

So, what is asynchronous?

This is how asynchronous works. It doesn’t pause everything just because one task takes time. Instead, it allows your program to continue doing other work while waiting for a task to complete thus making the performance more efficient and faster.

Top comments (0)