Understanding asynchronous operations
It's a word you've probably heard before, but may not understand exactly what it means or why we care. Asynchronous operations are operations that do not block while completing. What does that mean, exactly? Well, synchronous operations (the vast majority of commands in a program) get executed one at a time, in order. Consider this code:
mixBatter() bakeCake() frostCake()
Each of these functions is invoked in order, and not until the previous function returns. bakeCake
is only invoked after mixBatter
returns, and frostCake
is invoked only after bakeCake
returns. If bakeCake
takes 40 minutes to return, well, frostCake
will have to wait 40 minutes before it can run. We could say that bakeCake
blocks execution until it is finished.
This makes the code very easy to reason about, and most of the time it's the behavior that we want. However, there are times where we might wish to start an operation but not wait for it to finish before doing more work. Most often...