Asynchronous Programming as Seen at Starbucks

Published Oct 25, 2017Last updated Nov 21, 2017
Asynchronous Programming as Seen at Starbucks

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 computation for example, and the computer will take its time to prepare an answer, then deliver it back to you:

let primeNumbers100 = primeNumbersUnder(100);

After that line, you have your order ready and delivered in a bag labeled primeNumbers100.

Ordering in a drive-thru lane is what most programming languages do. You write a line of code (when you order something at the intercom stop), and that line of code will block the processor (the worker at the pickup window) until you receive the processor’s evaluation of your line of code (your ready order, at the window). Although the processor is capable of evaluating multiple lines of code (other orders placed after yours) during the time your line of code gets processed (order gets prepared), the fact that you’re in a single lane drive-thru (synchronous execution nature) will make that capability useless.

However, that capability is not useless inside at the standup line, because after you’re done with order, the workers can have you wait and call you when your order is ready. This is exactly how asynchronous programming works; the processor takes your order, then ignores you for a while, and when your order is ready, they call you back to receive it.

calculatePrimeNumbersUnder(100, callback)

When the calculation is ready, the callback will be invoked.

callback(primeNumbers100)

Think of the callback as your name, which you give at the standup order line. The Starbucks worker who took your order will associate your order with your name. When your order is ready, your name will be called, and you will be handed a beverage in that process. In the drive-thru lane, they do not ask you for you name, because your blocking location in the lane determines what you’ll be receiving.


I create online courses for Pluralsight and Lynda. My most recent courses are Advanced React.js, Advanced Node.js, and Learning Full-stack JavaScript. I also do online and onsite training for teams covering beginner to advanced levels in JavaScript, Node.js, React.js, and GraphQL. Email kyle@agilelabs.com if you want to book a session for your team.

This article was orginial published here

Discover and read more posts from Samer Buna
get started
Enjoy this post?

Leave a like and comment for Samer

14