The problem with waiting
In Python, code runs line by line. Each line finishes before the next one starts. When you callrequests.get(), your program stops and waits until the response comes back.
JavaScript is single-threaded
JavaScript runs on a single thread — one line of code at a time. If a network request takes 3 seconds and JavaScript just waited, your entire page would be frozen for 3 seconds.How async actually works
Think of it like ordering at a restaurant: Synchronous (Python script): You order food, stand at the counter staring at the kitchen, and don’t do anything else until your food arrives. Asynchronous (JavaScript): You order food, get a ticket number (a Promise), and sit down. You can check your phone, talk to friends — the restaurant will call your number when the food is ready.The event loop (simplified)
You don’t need to understand every detail of the event loop, but here’s the key idea:- JavaScript runs your code line by line on the main thread
- When it hits an async operation (network request, timer, etc.), it hands it off to the browser
- The browser handles the operation in the background
- When the operation completes, the result goes into a queue
- JavaScript picks up results from the queue when it’s done with the current code
setTimeout with 0 milliseconds doesn’t run immediately — it runs after the current code finishes. This proves that async callbacks always wait for the main thread to be free.What operations are async?
Not everything in JavaScript is async. Only operations that take an unpredictable amount of time:| Async (takes time) | Sync (instant) |
|---|---|
fetch() — network requests | Math operations |
setTimeout() / setInterval() | String manipulation |
| Reading files (in Node.js) | Array methods (.map(), .filter()) |
| Database queries | Object operations |
| User input events | Variable assignment |
Why this matters for you
As a Python developer, async is the biggest mental shift. In Python, you can mostly ignore async unless you’re usingasyncio. In JavaScript, you’ll deal with async in almost every component that fetches data.
The good news: JavaScript has clean syntax for handling async code. Over the next few lessons, you’ll learn the evolution:
- Callbacks — the original approach (messy)
- Promises — a better abstraction (cleaner)
- async/await — modern syntax (cleanest, what you’ll actually use)
What’s next?
Let’s start with the original async pattern — callbacks. Understanding callbacks helps you appreciate why Promises and async/await exist.Callbacks
The original async pattern and why we moved on