Asynchronous Programming as Seen at Starbucks

Samer Buna
EdgeCoders

--

You can order from Starbucks either by driving your car to a drive-thru lane or by going inside and waiting for your order there. In both cases, you are waiting for a resource: a worker to prepare your beverage.

There is a subtle but big difference between the 2 ways you can order. When you line up your car in the single lane drive-thru, you are blocking every one behind you. No one behind you can get their order before you do. However, when you place your order inside then step aside to wait for it, the lady who ordered after you might receive her order before you, for example, if you ordered a latte and she ordered a regular coffee.

Since there are multiple workers at Starbucks who can process multiple orders in parallel, there is no guarantee about the order of deliveries. However, in the drive-thru lane, even if the smaller order from the car behind you was prepared faster than yours, they still have to wait for you to clear the lane before they can pick up their order from the single window where orders can be picked up. If the car in front of you ordered 12 beverages, you will be waiting in your car for a while, and your order might actually be waiting for you as well at the window.

Think of your order at Starbucks as a line of code in your program. You tell the computer to do something for you, perform a…

--

--

Samer Buna
EdgeCoders

Author for Pluralsight, O'Reilly, Manning, and LinkedIn Learning. Curator of jsComplete.com