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.