Performance: Async/await error handling patterns
MEDIUM IMPACT
This affects how asynchronous errors impact event loop responsiveness and error propagation speed in Node.js applications.
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); throw error; // propagate error properly } } fetchData().then(console.log).catch(console.error);
async function fetchData() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return json; } fetchData().then(console.log).catch(console.error);
| Pattern | Event Loop Blocking | Error Handling Speed | Parallelism | Verdict |
|---|---|---|---|---|
| Sequential awaits without try/catch | High (multiple awaits block event loop) | Slow (errors caught late) | No | [X] Bad |
| Try/catch inside async function | Low (errors caught immediately) | Fast (immediate error handling) | No | [OK] Good |
| Sequential awaits in loop with try/catch | High (multiple awaits block event loop) | Moderate | No | [!] OK |
| Parallel async calls with Promise.all and try/catch | Low (single event loop wait) | Fast (errors caught early) | Yes | [OK] Good |