Dùng try/catch bọc await expressions.
Lỗi từ rejected Promise được catch như exception.
javascript
async function loadData() {
try {
const res = await fetch('/api/data');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
} catch (err) {
console.error('Failed:', err.message);
return null;
} finally {
hideLoading(); // luôn chạy
}
}
// Hoặc chain .catch() bên ngoài
loadData().catch(err => console.error(err));
// Parallel: cần Promise.all để catch cả hai
const [a, b] = await Promise.all([fetchA(), fetchB()]);Use try/catch to wrap await expressions.
Errors from rejected Promises are caught like exceptions.
javascript
async function loadData() {
try {
const res = await fetch('/api/data');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
} catch (err) {
console.error('Failed:', err.message);
return null;
} finally {
hideLoading(); // always runs
}
}
// Or chain .catch() from the outside
loadData().catch(err => console.error(err));
// Parallel: use Promise.all to catch both errors
const [a, b] = await Promise.all([fetchA(), fetchB()]);