Why Async?
We generally try to write synchronous code whenever possible because it is deterministic, easy to read, and simple to keep track of—which naturally leads to fewer bugs.
So why do we need asynchronous code at all?
The Problem with Blocking Code
Sometimes our code needs to perform operations that take an unpredictable amount of time.
For example, whenever you update your user settings on a website, your browser must send an HTTP request across the internet to a server. The time it takes for that data to physically travel back and forth can range anywhere from 10 to 1000+ milliseconds.
If our programming language strictly forced synchronous execution, the entire webpage would freeze (“block”) while waiting for that HTTP response to come back. You wouldn’t be able to click buttons, scroll, or type.
The Asynchronous Solution
By executing slow operations (like network requests) asynchronously, the application hands the slow task off to the background.
The main thread is instantly freed up, allowing the webpage to remain highly responsive (rendering UI updates, handling user clicks) while waiting for the HTTP response to eventually arrive and trigger its associated callback.