Middle
What happens if errors are not handled in a Promise (no .catch)?
sobes.tech AI
Answer from AI
If an error in a Promise is not handled with .catch(), the error will remain unhandled. In modern environments (such as browsers or Node.js), this will result in a warning or an error "Unhandled Promise Rejection".
Consequences:
- In the browser, a message may appear in the console indicating that the error was not handled.
- In Node.js, unhandled promise rejections may cause the process to terminate with an error (depending on the version and settings).
This is bad practice because errors can go unnoticed, making debugging difficult and potentially leading to unpredictable application behavior.
Example of proper handling:
fetch('/api/data')
.then(response => response.json())
.catch(error => {
console.error('Error loading data:', error);
});