Performance: Error handling in async/await
This affects interaction responsiveness and error recovery speed during asynchronous operations in Node.js applications.
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); throw error; // rethrow or handle gracefully } } 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 | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No internal try/catch in async function | N/A | N/A | N/A | [X] Bad |
| Using try/catch inside async function | N/A | N/A | N/A | [OK] Good |
try/catch blocks with async/await in Node.js?async function getData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
} catch (error) {
return 'Error occurred';
}
}
getData().then(console.log);async function loadUser() {
try {
const user = fetch('https://api.example.com/user');
console.log(user.name);
} catch (err) {
console.error(err);
}
}async function getUserPosts(userId) {
// Your code here
}