🧠 Async vs Await vs Java Threads
Why JavaScript Feels Like Magic (and Why It’s Not)
Modern developers often stumble into a strange realization:
“Why does JavaScript not block… even when it looks like it should?”
If you’re coming from Java, this feels like stepping from a stone temple into a floating garden.
Let’s ground it.
☕ Java: The World of Threads
In Java, execution is straightforward:
System.out.println("1");
Thread.sleep(2000);
System.out.println("2");
The thread pauses. Everything waits.
Java’s philosophy is simple:
One thread, one path, one pause at a time.
If you want concurrency, you must explicitly summon it:
ExecutorServiceCompletableFuture- custom thread management
Async is opt-in and carries weight.
🟨 JavaScript: The Illusion of Parallelism
JavaScript lives differently.
console.log("1");
setTimeout(() => {
console.log("2");
}, 2000);
console.log("3");
Output:
1
3
(wait)
2
There is no waiting. No blocking. Just scheduling.
JavaScript says:
“I’ll handle that later. What’s next?”
🎭 Enter async / await
Now comes the part that confuses everyone.
await page.goto("https://example.com");
It looks like blocking code.
But it’s not.
🧠 What await Actually Does
When you write:
await somethingAsync();
You’re saying:
“Pause this function until the result is ready… but let everything else keep running.”
This is crucial.
- The function pauses ❗
- The thread does NOT ❗
JavaScript continues processing:
- events
- timers
- UI updates
🧩 Promises: The Hidden Engine
Behind every await is a Promise.
A Promise is simply:
“I don’t have the result yet, but I will.”
const result = page.goto("...");
This doesn’t return the result.
It returns a promise of a result.
await unwraps that promise.
⚔️ Java vs JavaScript
In Java, async requires explicit effort:
CompletableFuture.runAsync(() -> {
doSomething();
});You opt into async.
| Concept | Java | JavaScript |
|---|---|---|
| Default behavior | Blocking | Non-blocking |
| Concurrency model | Multi-threaded | Single-threaded |
| Async control | Explicit | Built-in |
| Waiting | Stops thread | Pauses function only |
🍜 A Simple Analogy
- Java: You wait for your ramen 🍜 before doing anything else
- JavaScript: You order ramen, then check your phone, talk to friends, and come back when it’s ready
🤖 Why This Matters for Playwright
Browser automation is full of delays:
- page loads 🌍
- network calls 📡
- DOM updates 🧩
Without await, your script becomes chaos:
page.goto("...");
page.fill("#name", "John");
You’re typing before the page exists.
With await:
await page.goto("...");
await page.fill("#name", "John");
Now your bot behaves like a patient human.
🧘 The Mental Shift
If you come from Java, stop asking:
“Is this blocking?”
Instead ask:
“Is this asynchronous, and did I await it?”
⚡ Final Thought
Java controls threads.
JavaScript controls time.
And async/await is simply your way of moving through time… without tripping over it.