All tutorials
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
done

4. 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

  1. Convert a .then chain to async/await
  2. Use try/catch around an awaited rejection
  3. Use Promise.all with map to fetch many URLs in parallel
→ Try more practice problems