0
0
Javascriptprogramming~5 mins

Error handling with async and await in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afor loop
Bif...else
Cswitch
Dtry...catch
What happens if an awaited promise rejects and there is no try...catch?
AThe program crashes or throws an unhandled rejection
BThe error is ignored
CThe promise resolves anyway
DThe code continues silently
Which of these is a correct way to catch errors in async/await?
Aawait fetchData().catch(error => console.log(error))
Btry { await fetchData() } catch (error) { console.log(error) }
CBoth A and B
DNeither A nor B
Why is try...catch preferred inside async functions over .then().catch()?
AIt runs faster
BIt makes code look synchronous and easier to read
CIt uses less memory
DIt disables errors
What does the 'async' keyword do?
AMarks a function to always return a promise
BMakes a function synchronous
CPrevents errors
DRuns code faster
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.