JavaScript Async/Await

Write asynchronous code that reads like normal code.

✍️ 3 exercises + code challenge in this lesson

async and await make promise-based code easier to read.

  • An async function always returns a promise.
  • await pauses the function until the promise settles, then gives you its result.
  • Handle errors with a normal try...catch.

Key Takeaways: JavaScript Async/Await

Mastering JavaScript Async/Await helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript Async/Await Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function makeTea() {
  try {
    log("Boiling water...");
    await wait(800);
    log("Adding tea leaves...");
    await wait(800);
    log("Adding milk and sugar...");
    await wait(800);
    return "Chai is ready ☕";
  } catch (e) {
    return "Something went wrong";
  }
}

makeTea().then(result => log(result));
log("(This line runs first: makeTea does not block the page.)");
</script>
Free sign-in required · Google login available

JavaScript Async/Await Practice Exercises

Check what you learned in “JavaScript Async/Await”.

/ 3 solved
Fill in the blank

Mark the function as asynchronous.

 function load() {
  const data = await getData();
}
✓ Correct! Not quite. Try again.
Multiple choice

What does an async function always return?

✓ Correct! Not quite. Try again.
Code challenge

Write an async function getValue() that returns 10, then await it inside another async function and log the result.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services