0
0
Javascriptprogramming~5 mins

Then and catch methods in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the then method do in JavaScript promises?
The then method runs a function when a promise is successfully completed (resolved). It lets you handle the result of the promise.
Click to reveal answer
beginner
What is the purpose of the catch method in promises?
The catch method runs a function when a promise fails (is rejected). It helps you handle errors or problems.
Click to reveal answer
intermediate
How do then and catch work together in promise chains?
You use then to handle success and catch to handle errors. This way, you can manage both outcomes in a clear, step-by-step way.
Click to reveal answer
beginner
What happens if you don’t use catch with a promise?
If you don’t use catch, errors in the promise might go unnoticed, causing bugs or unhandled promise rejections.
Click to reveal answer
beginner
Show a simple example of using then and catch with a promise.
Example:<br>
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
Click to reveal answer
What does the then method do in a promise?
AHandles the successful result of the promise
BHandles errors in the promise
CStops the promise from running
DCreates a new promise
Which method is used to catch errors in a promise chain?
Afinally
Bthen
Ccatch
Dresolve
What will happen if a promise is rejected but there is no catch method?
AThe promise will automatically retry
BThe program will stop running
CThe <code>then</code> method will handle the error
DThe error will be ignored and may cause unhandled rejection warnings
Can you chain multiple then methods?
ANo, chaining is only for <code>catch</code>
BYes, to handle multiple steps after a promise resolves
CYes, but only if you use <code>catch</code> first
DNo, only one <code>then</code> is allowed
What does this code do?<br>
promise.then(fn1).catch(fn2);
ARuns fn1 if promise resolves, fn2 if promise rejects
BRuns fn2 if promise resolves, fn1 if promise rejects
CRuns both fn1 and fn2 regardless of promise result
DRuns neither fn1 nor fn2
Explain how then and catch methods help manage asynchronous code in JavaScript.
Think about how you handle good and bad outcomes in real life.
You got /4 concepts.
    Describe a simple example where you would use then and catch with a promise.
    Imagine getting data from the internet and handling success or failure.
    You got /4 concepts.