async and await make promise-based code easier to read.
- An
asyncfunction always returns a promise. awaitpauses the function until the promise settles, then gives you its result.- Handle errors with a normal
try...catch.
Write asynchronous code that reads like normal code.
✍️ 3 exercises + code challenge in this lessonasync and await make promise-based code easier to read.
async function always returns a promise.await pauses the function until the promise settles, then gives you its result.try...catch.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.
<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>
Check what you learned in “JavaScript Async/Await”.
Mark the function as asynchronous.
function load() {
const data = await getData();
}
What does an async function always return?
Write an async function getValue() that returns 10, then await it inside another async function and log the result.
Free sign-in required · Google login available
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