🧭Async/Await
Also known as async await, async, await
Async code that reads top to bottom. await waits for each result.
Think of it like
Think of it like a recipe that says wait for the oven. You read it top to bottom, like any recipe.
Example
Emma writes const data = await fetch(url). The next line can use data, and the page never freezes while it waits.
Why it matters
It is the most common way to write async code today. It is far easier to read than chains of callbacks.
Definition
Example code
javascript
async function load() {
const res = await fetch("/api/user");
return await res.json();
}Common mistake
Key takeaways
- 1Reads in order. Like normal code.
- 2await pauses. Only this function.
- 3Built on promises. Same thing underneath.