Advanced
Async / Await
Write asynchronous code that reads like synchronous code.
1. Explanation
Mark a function with 'async' to make it return a Promise. Inside, use 'await' before a Promise to pause execution until it resolves. Wrap awaits in try/catch to handle errors. Cleaner than .then chains for sequential async work.
2. Example
Try it yourself
Console
Click Run to see output.
3. Expected output
output
start
after 0.5s
done4. Common mistakes
- ✗Forgetting 'await' — the function returns a pending promise
- ✗Awaiting in a loop when Promise.all would parallelise
- ✗Using await at the top level outside a module that supports it
5. Practice questions
- Convert a .then chain to async/await
- Use try/catch around an awaited rejection
- Use Promise.all with map to fetch many URLs in parallel