Quiz
Warm-up from lesson 5-2: inside a `.then` callback you `return delay(20, "x")`, which is a promise. What does the next `.then` receive?
await is chain syntax
Two keywords rewrite promise chains as ordinary-looking lines:
asyncbefore a function makes it always return a promise.return 5inside it fulfills that promise with 5.await promise(only inside async functions) pauses the function until the promise settles, then hands you the unwrapped value.
Crucial nuance: await pauses that one function, not the program. The rest of your script keeps running, and the paused function resumes later as a microtask (lesson 4-2).
Why the syntax exists: .then chains still read inside-out once conditions, loops, or retries get involved — try writing "attempt three times, then fall back" as a chain. With await, async code uses the same if/for/try tools as everything else, which is why it is the default style in modern codebases and the one interviewers expect.
Code exercise · javascript
Run it. Note the order: `main` pauses at the await, the script continues past `main()`, and the awaited value arrives last.
Errors: plain try/catch
With await, a rejected promise throws at the await line. So error handling is the try/catch you already know, instead of .catch chains from lesson 5-3:
async function load() { try { const user = await fetchUser(42); console.log(user); } catch (err) { console.log("fallback: guest"); } }
Same rejection, same recovery, straighter code. This is the style most codebases and interviewers expect from you by default.
Code exercise · javascript
Run it. Same fetchUser as lesson 5-3: id 1 resolves and prints normally; id 42 rejects, the rejection surfaces as a thrown error at the await line, and the catch recovers.
Quiz
An `async` function's body is just `return 7;`. What does calling it give you?
Code exercise · javascript
Your turn. Rewrite the `.then` chain in `main` using async/await: await the user, uppercase it, print `hello ADA`. Keep the output identical.