What does await do inside an async function?
async function loadUser() {
const res = await fetch("/api/user");
const user = await res.json();
return user;
}Options
- It blocks the whole browser until the request ends
- It pauses this function until the promise settles (Correct option)
- It turns the request into a synchronous request
- It retries the request until it finally works
Answer
Pauses this function until the promise settles, without freezing the rest of the page
Why
await waits for a promise and gives you its value, so async code reads top to bottom like normal code.
Only THIS function pauses; the browser keeps handling clicks, animations and other code meanwhile.
If the promise rejects, await throws, so wrap it in try/catch to handle errors. Note: an async function itself always returns a promise.
Remember
await = pause this function, not the page.