Complete the code to print an error message when a promise is rejected.
fetch('https://api.example.com/data') .then(response => response.json()) .catch(error => console.[1](error))
Use console.error to clearly show error messages in Node.js debugging.
Complete the code to catch and log errors in an async function.
async function getData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
} catch ([1]) {
console.error(error);
}
}The catch block uses error as the variable name to hold the caught error.
Fix the error in the code to properly handle rejected promises.
fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)) .[1](error => console.error(error))
The correct method to handle rejected promises is catch.
Fill both blanks to create a simple error handler function.
function handleError([1]) { console.[2](error.message); }
The function takes error as parameter and logs the message using console.error.
Fill all three blanks to log error details conditionally.
function logErrorDetails([1]) { if (error.[2]) { console.[3](`Error: ${error.message}`); } }
The function checks if the error has a stack trace and logs the message using console.error.
