Performance: Why robust error handling matters
This affects the responsiveness and stability of a Node.js application, impacting how quickly errors are detected and handled without blocking the event loop.
Jump into concepts and practice - no test required
async function fetchData() { try { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return json; } catch (error) { console.error('Fetch failed:', error); return null; } } fetchData().then(console.log);
async function fetchData() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return json; } fetchData().then(console.log);
| Pattern | Event Loop Impact | Error Detection Speed | App Stability | Verdict |
|---|---|---|---|---|
| No error handling in async code | Blocks event loop on unhandled rejections | Slow - errors may crash app | Low - unstable | [X] Bad |
| Try-catch in async functions | Keeps event loop free | Fast - errors caught early | High - stable | [OK] Good |
| Ignoring errors in callbacks | Delays error handling, possible blocking | Slow - silent failures | Low - unstable | [X] Bad |
| Proper error checks in callbacks | Non-blocking, immediate handling | Fast - immediate detection | High - stable | [OK] Good |
try { } catch (error) { } structure.try {
throw new Error('Oops!');
} catch (e) {
console.log('Caught:', e.message);
} finally {
console.log('Done');
}throw triggers an error caught by catch, which logs 'Caught: Oops!'.finally block always runs, logging 'Done' after the catch.try {
console.log('Start');
throw 'Error happened';
} catch {
console.log('Caught an error');
}catch (error).