JavaScript › Error Handling
How do you catch errors from a Promise chain versus async/await?
IntermediateAnswer
With .then()/.catch() chains, attach a .catch() at the end to handle rejections from any prior step. With async/await, wrap the awaited calls in a try/catch block — both approaches ultimately catch the same underlying rejected Promise.
// Promise chain
fetchData().then(process).catch(err => console.error(err));
// async/await
try {
const data = await fetchData();
process(data);
} catch (err) {
console.error(err);
}Real-world example Handling a failed API call gracefully instead of an unhandled promise rejection crashing the app.