Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to catch errors from the promise.
Node.js
fetchData().then(data => console.log(data)).[1](error => console.error(error)); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'then' instead of 'catch' to handle errors.
Using 'finally' to catch errors (it runs regardless of success or failure).
✗ Incorrect
The catch method is used to handle errors from promises.
2fill in blank
mediumComplete the code to handle errors using async/await with try/catch.
Node.js
async function load() {
try {
const data = await fetchData();
console.log(data);
} catch ([1]) {
console.error(error);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not represent an error.
Leaving the catch parameter empty.
✗ Incorrect
The catch block receives the error object, commonly named error.
3fill in blank
hardFix the error in the promise chain to properly catch errors.
Node.js
fetchData() .then(data => console.log(data)) .[1](error => console.log('Error:', error));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'then' to catch errors causes errors to be unhandled.
Using 'error' as a method name causes syntax errors.
✗ Incorrect
The catch method must be used to catch errors in a promise chain.
4fill in blank
hardFill both blanks to handle errors and always run cleanup code.
Node.js
fetchData() .then(data => console.log(data)) .[1](error => console.error(error)) .[2](() => console.log('Cleanup done'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'then' instead of 'catch' for errors.
Not using 'finally' for cleanup.
✗ Incorrect
Use catch to handle errors and finally to run code regardless of success or failure.
5fill in blank
hardFill all three blanks to create a promise chain that logs data, catches errors, and always logs completion.
Node.js
fetchData() .[1](data => console.log(data)) .[2](error => console.error('Error:', error)) .[3](() => console.log('Done'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of then, catch, and finally.
Using 'resolve' which is not a method on promises.
✗ Incorrect
The promise chain uses then to handle success, catch for errors, and finally for code that runs always.