⏳ Async / Await

JavaScript Guide

The Science Behind JavaScript Saying “Hold On a Second”

Why does everything catch fire when you forget await? 🔥

Writing asynchronous code in JavaScript is a bit like this:

You’re trying to cook at the same time,
the tea is brewing,
the doorbell rings,
the phone rings,
someone shouts “the Wi-Fi is down!”…

JavaScript works exactly like this.
And async / await is the traffic police inside that chaos 🚦


🧠 1️⃣ JavaScript’s Personality Analysis (Very Important!)

First, let’s understand JS—because the problem isn’t the code…
it’s the personality.

JavaScript is:

  • 🧵 Single-threaded (does one thing at a time)
  • Fast
  • 😤 Hates waiting
  • 🧠 Has a “we’ll deal with this later” mindset

So JS loves this:

console.log("1");
console.log("2");
console.log("3");

📤 Output:

1
2
3

Peace. Zen. Minimalism. 🧘‍♂️


💥 2️⃣ Why Does Everything Break When Async Code Enters?

Now let’s throw an async task onto the stage:

console.log("Started");

setTimeout(() => {
  console.log("Finished");
}, 2000);

console.log("Continuing");

📤 Output:

Started
Continuing
Finished

😲 “Wait… shouldn’t it pause for 2 seconds?”

JavaScript answers calmly:

“I don’t wait.
You wait.”

📌 Because:

  • setTimeout → asynchronous
  • JS → doesn’t want to be blocked
  • Waiting is outsourced to Web APIs

🧩 3️⃣ What Is a Promise? (JavaScript’s “I Promise”)

A Promise means:

“It’s not here yet…
but it will be, I promise.”

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Data arrived 🎉");
  }, 2000);
});

📌 A Promise has 3 states:

StateMeaning
pendingWaiting ⏳
fulfilledDone ✅
rejectedOops ❌

But here’s the critical part:

A Promise is not the result
A Promise = a document saying the result will arrive


😈 4️⃣ The Real Disaster: Forgetting await

The classic mistake:

async function getData() {
  const data = fetch("https://api.example.com/data");
  console.log(data);
}

getData();

📤 Output:

Promise { <pending> }

🔥🔥🔥
“WHERE IS MY DATA?!”

JavaScript stays calm:

“I didn’t give you data.
I gave you a receipt.”


🧠 5️⃣ What Does await Do? (The Life-Saving Line)

await means:

“Stop.
Wait until this Promise finishes.
Then continue.”

Correct usage:

async function getData() {
  const response = await fetch("https://api.example.com/data");
  const data = await response.json();
  console.log(data);
}

📌 Step by step:

1️⃣ fetch starts
2️⃣ await → JS pauses here
3️⃣ Promise resolves
4️⃣ Real data arrives
5️⃣ Console: 🎉


🧪 6️⃣ Mini Experiment: With vs Without await

❌ Without await

async function example() {
  const result = Promise.resolve(42);
  console.log(result);
}

📤 Output:

Promise { 42 }

✅ With await

async function example() {
  const result = await Promise.resolve(42);
  console.log(result);
}

📤 Output:

42

🎯 Quick summary:

  • No await → promise
  • With await → reality

🤯 7️⃣ “Why Is Everything On Fire?”

A very familiar scenario:

const user = await getUser();
console.log(user.name);

But if you forget await:

const user = getUser();
console.log(user.name);

📤 Result:

TypeError: Cannot read property 'name'

🔥🔥🔥

📌 Because:

  • user = Promise
  • Promises don’t have a name

JavaScript says:

“You gave me a Promise,
so I treated it like one.”


⚠️ 8️⃣ Most Common Async / Await Mistakes

❌ Using await without async

function test() {
  await fetch(url); // ❌
}

❌ Treating a Promise like a normal value
❌ Using await inside map

items.map(async item => {
  await doSomething(item);
});

📌 This does not wait!


🚀 9️⃣ Escaping the Async map Trap

❌ Wrong

items.map(async item => {
  await process(item);
});

✅ Correct

await Promise.all(
  items.map(item => process(item))
);

📌 Promise.all:

  • Starts everything
  • Waits for everything
  • Then continues

🎯 Performance + control = ❤️


🛡️ 🔟 Never Write Async Without try / catch

Async code = possible failure.

async function loadData() {
  try {
    const res = await fetch(url);
    const data = await res.json();
    console.log(data);
  } catch (error) {
    console.error("Something burned 🔥", error);
  }
}

📌 try/catch = fire extinguisher 🧯
📌 Async without try/catch = lighting a grill with gasoline


🧠 1️⃣1️⃣ Golden Rules (Real-World Tips)

✅ Be suspicious of anything returning a Promise
✅ Want the result? Use await
✅ Parallel work → Promise.all
✅ Readability > clever code
✅ While debugging: console.log(await value)


🏁 FINAL WORD: What JavaScript Is Telling You

JavaScript says:

“I’m fast.
But if you don’t teach me to wait,
I will never stop.”

And remember:

If you forget await
the code runs…
but logic doesn’t 😏

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir