Recall & Review
beginner
What is the purpose of using
try...catch with async/await?It helps catch errors that happen during the asynchronous operation, allowing the program to handle them gracefully instead of crashing.
Click to reveal answer
beginner
How do you write an async function that handles errors?
Use
async before the function, and wrap the await calls inside a try block. Use catch to handle any errors thrown.Click to reveal answer
beginner
What happens if you don't use
try...catch around an await expression that rejects?The error will be unhandled and may cause the program to crash or behave unexpectedly.
Click to reveal answer
intermediate
Can you use
.catch() with async/await?Yes, you can attach
.catch() to the promise returned by an async function, but using try...catch inside the async function is often clearer.Click to reveal answer
intermediate
Why is
async/await preferred over .then() and .catch() for error handling?async/await makes asynchronous code look like normal synchronous code, which is easier to read and write, especially when handling errors with try...catch.Click to reveal answer
What keyword do you use to handle errors in an async function?
✗ Incorrect
try...catch blocks are used to catch errors in async functions when using await.
What happens if an awaited promise rejects and there is no try...catch?
✗ Incorrect
Without try...catch, rejected promises cause unhandled errors that can crash the program.
Which of these is a correct way to catch errors in async/await?
✗ Incorrect
Both attaching .catch() to the promise and using try...catch inside async functions are valid.
Why is try...catch preferred inside async functions over .then().catch()?
✗ Incorrect
try...catch with async/await makes asynchronous code easier to understand by looking like normal code.
What does the 'async' keyword do?
✗ Incorrect
An async function always returns a promise, allowing use of await inside it.
Explain how to handle errors when using async and await in JavaScript.
Think about how you catch errors in normal code and apply it to async code.
You got /4 concepts.
Describe the difference between using .catch() on a promise and using try...catch with async/await.
Consider how the code looks and where the error handling happens.
You got /4 concepts.