Complete try/catch for async
Fill in the Blank Medium
Question
Fill in the blank to catch errors from the awaited fetch call.
Code
async function load() {
___ {
const res = await fetch("/api/data");
return await res.json();
} catch (err) {
console.error("Failed:", err);
}
}
Hint
Use the keyword that starts a try/catch block.
Explanation
try begins a try/catch block. If any await inside the try block rejects (throws), execution jumps to the catch block with the error. This is the standard way to handle errors in async functions — it makes async error handling look like synchronous try/catch.
Related Resources
Related Lessons
- Async JavaScript
Learn about try/catch with async/await.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.