Performance: Promise catch for async errors
This concept affects how asynchronous errors are handled without blocking the event loop or causing unhandled promise rejections.
Jump into concepts and practice - no test required
async function fetchData() { try { const response = await fetch('https://api.example.com/data'); const data = await response.json(); return data; } catch (error) { console.error('Fetch failed:', error); return null; } } fetchData();
async function fetchData() { const response = await fetch('https://api.example.com/data'); const data = await response.json(); return data; } fetchData(); // No catch or error handling
| Pattern | Error Handling | Event Loop Impact | Process Stability | Verdict |
|---|---|---|---|---|
| No .catch() on Promise | None | Unhandled rejection may crash process | May crash process | [X] Bad |
| Using .catch() on Promise | Proper | Keeps event loop free | Stable process | [OK] Good |
| Async/await without try/catch | None | Unhandled rejections possible | Unstable | [X] Bad |
| Async/await with try/catch | Proper | Stable event loop | Stable process | [OK] Good |
catch with a Promise in Node.js?catch does in Promisescatch method is designed to handle any errors that happen during the execution of a Promise or in the chain of Promises.catch in error handlingthen callbacks, preventing unhandled errors.catch = error handler [OK]catch always handles errors in promises [OK]catch starts the async operationcatch with success handlerscatch makes code synchronousmyPromise?catch, which takes a callback for the error.catch correctly with an arrow function to log the error.catch for errors in Promises [OK].catch() after Promise to handle errors [OK]then to catch errorserror methodfinally with error handlingPromise.reject('Error happened')
.catch(err => console.log('Caught:', err));Promise.reject('Error happened') creates a rejected Promise with the error message 'Error happened'.catch method receives the error and logs it prefixed with 'Caught:'.catch callback [OK]fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(console.error());catch is usedconsole.error() immediately and passes its result (undefined) to catch, which expects a function.catch usagecatch should receive a function reference or arrow function, like catch(error => console.error(error)).catch method is called with the result of console.error() instead of a function -> Option Dcatch, not call it [OK]catch, don't call it immediately [OK]console.error() inside catch instead of passing functionfetch returns a Promiseresponse.json() callasync function getData() {
return Promise.reject('Failed to load');
}
getData()
.then(data => console.log('Data:', data))
.catch(err => {
console.log('Error caught:', err);
return 'Default data';
})
.then(result => console.log('Result:', result));getDatathen is skipped and control goes to catch.catch and subsequent thencatch logs the error and returns 'Default data', which resolves the Promise chain. The next then receives this value and logs it.catch handles error and returns value for next then [OK]catch goes to next then [OK]catch