📖 Scenario: You are building a simple web app that fetches user data from a server. Sometimes the server responds successfully, and sometimes it fails. You want to handle both cases properly.
🎯 Goal: Learn how to use then and catch methods to handle successful and failed promise results.
📋 What You'll Learn
Create a promise that simulates fetching user data
Create a variable to hold a success message
Use then to handle the success case
Use catch to handle the error case
Print the final message to the console
💡 Why This Matters
🌍 Real World
Handling promises is essential when working with data from servers or APIs that may take time to respond or may fail.
💼 Career
Understanding then and catch methods is important for frontend and backend developers to manage asynchronous operations and errors gracefully.
Progress0 / 4 steps
1
Create a Promise to simulate fetching user data
Create a constant called fetchUserData that is a new Promise. Inside the Promise, use resolve to send the string 'User data loaded'.
Javascript
Hint
Use new Promise((resolve, reject) => { ... }) and call resolve with the success message.
2
Create a variable to hold the success message
Create a variable called message and set it to an empty string ''.
Javascript
Hint
Use let message = ''; to create an empty string variable.
3
Use then and catch methods to handle the Promise
Use fetchUserData.then with a function that sets message to the resolved value. Then use fetchUserData.catch with a function that sets message to 'Error loading user data'.
Javascript
Hint
Use fetchUserData.then(result => { message = result; }) and fetchUserData.catch(error => { message = 'Error loading user data'; }).
4
Print the final message to the console
Write console.log(message) to print the value of message.
Javascript
Hint
Use console.log(message) to show the message. Because promises run asynchronously, use setTimeout to delay the print.
Practice
(1/5)
1. What does the then method do in JavaScript promises?
easy
A. Creates a new promise
B. Runs when the task fails
C. Stops the promise from running
D. Runs when the task finishes successfully
Solution
Step 1: Understand the role of then
The then method is used to handle the result when a promise completes successfully.
Step 2: Differentiate from catch
The catch method handles errors or failures, not success.
Final Answer:
Runs when the task finishes successfully -> Option D
Quick Check:
then = success handler [OK]
Hint: Remember: then = success, catch = failure [OK]
Common Mistakes:
Confusing then with catch
Thinking then stops the promise
Believing then creates a new promise
2. Which of the following is the correct syntax to add a catch handler to a promise named p?
easy
A. catch(p, () => console.log('Error'))
B. p.then.catch(() => console.log('Error'))
C. p.catch(() => console.log('Error'))
D. p.catch = () => console.log('Error')
Solution
Step 1: Recall correct catch syntax
The catch method is called directly on the promise object like p.catch(callback).
Step 2: Check each option
p.catch(() => console.log('Error')) uses correct syntax. p.then.catch(() => console.log('Error')) chains incorrectly. p.catch = () => console.log('Error') tries to assign catch as a property. catch(p, () => console.log('Error')) uses invalid function call.
Final Answer:
p.catch(() => console.log('Error')) -> Option C
Quick Check:
Use promise.catch() syntax [OK]
Hint: Use promise.catch(callback) to handle errors [OK]