How asynchronicity gets you a nice girl

I just made myself some pasta. Ok, nothing spectacular so far, just pasta with plain tomato sauce. But while doing so, I thought about the way I was acting and recognized the similarity between my pasta cooking and …

… JavaScript. Yes, JavaScript.

Let me explain. What’s the “algorithm” for making pasta with tomato sauce? It’s something like this (may vary depending on your favored pasta style):

  1. Put salt water on the stove.
  2. While the water heats, put some oil into a frying pan.
  3. While the oil heats, cut an onion. The water is still heating.
  4. Fry the onion while you cut some tomatoes.
  5. Add tomatoes to the onions.
  6. Your water could be boiling by now, so add pasta to the water.
  7. Let the sauce loose some water by boiling it for a while.
  8. Then add some spices.
  9. Take the pasta pout of the water, when it’s finished (This will probably take the most time).
  10. Add sauce.
  11. Eat it and Be happy.

You see, there’s a lot of stuff happening concurrently although you are doing it alone - “single threaded”, so to speak. This is very much like the asynchronous calls that are common in JavaScript and some other languages. The whole process just takes as long as its longest part, which is probably boiling the pasta.

Now image making pasta synchronously, like you would program in the more traditional way:

  1. Put water on the stove.
  2. Wait until it boils
  3. Put the pasta in the water
  4. Wait until its ready.
  5. Cut the onion
  6. Put oil into the pan.
  7. Wait …
  8. Put the onion into the hot oil.
  9. Wait …

You see where this is going. There’s a lot of waiting involved. That’s time in which you could have done something useful. Like inviting a nice girl over to share the pasta with. But sadly, because you chose synchronous cooking, you’ll eat not only much later but also alone. And your pasta will most likely have gotten cold while you were so busy with idling.

Now think again about how “complicated” and “hard to grasp” asynchronous programming really is.

PS: I didn’t invite anyone over for pasta because I used the free time that I had because of asynchronous cooking to write this post ;)

Maybe I should set some different priorities …

PPS: And don’t get me started on multithreaded cooking. My kitchen is barely big enough for one person. Just like your computers RAM is just not big enough to be wasted on threads.