JavaScript › Error Handling

How do you catch errors from a Promise chain versus async/await?

Intermediate

Answer

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.

Common follow-ups: What happens to errors thrown inside a .then() callback — are they caught by a later .catch()?