async / await — *Async Like Sync*
async / await — *Async Like Sync*
🎯 After Reading This Lesson
By the end of this lesson, you will be able to confidently do the following 3 things.
- ▸✅ Why async / await is syntactic sugar over Promise
- ▸✅ Why await inside forEach doesn't work + fix with for-of
- ▸✅ Implementing a fetch timeout with AbortController
Keep the learning goals as a checklist — close the lesson only when you can answer all of them.
async / await
The Core in One Line
async/await (ES2017) = syntax for writing Promises like synchronous code. Far more readable than then-chaining. The standard in modern JS.
Comparison
Almost identical in shape to synchronous code. Reads linearly.
Two Rules
1. await only inside an async function:
2. An async function always returns a Promise:
Error Handling — try-catch
Use the familiar try-catch instead of then-catch. Exception flow is identical to regular code.
Parallelism — The Sequential await Trap
When requests are independent of each other, always parallelize.
Real-World Pattern — Retry
Top-level await — In Modules
Since ES2022, await is available at the top level of modules:
Supported in Node.js and modern browsers (modules). Keeps entry points clean.
In React Components
Quick Summary
- ▸
async function+await Promise= async that feels synchronous - ▸Errors handled with try-catch
- ▸Independent requests go parallel with Promise.all
- ▸The modern JS standard — then-chaining is rarely used anymore
⚡ Try It Yourself — async / await + Sequential vs. Parallel
🤖 Try Asking AI Like This
Knowing the concepts from this lesson lets you give AI specific instructions. Not a vague 'fix this' but a request with vocabulary — that's where token savings begin.
- ▸'This await inside forEach isn't working — convert it to for...of or Promise.all'
- ▸'Add an AbortController timeout to this fetch'
- ▸'Add try-catch with a friendly error message to this function'
Why This Saves Tokens
Without understanding the concepts, you have to ask 'What does that mean?' after every AI response. Those follow-up questions eat up tokens. Learn the concept once and the conversation wraps up in a single exchange.