JavaScript · Promises · Junior · Best Practice
How should errors be handled with Promises vs async/await?
Short Interview Answer
Use `.catch` / trailing rejection handlers on chains; use try/catch around `await`. Always ensure rejections are handled to avoid unhandledrejection.
Detailed Explanation
Rejected promises propagate down the chain until caught. Mixing styles is fine: `await` turns rejections into exceptions. `try/catch` does not catch errors inside a floating promise without await. `.finally` runs on settle for cleanup but does not absorb errors unless it throws/rejects. In browsers/Node, unhandled rejections emit events and may crash Node depending on version/settings. Prefer catching at meaningful boundaries and rethrowing with context.
Example
async function load() {
try {
return await fetch('/x').then((r) => r.json());
} catch (err) {
console.error('load failed', err);
throw err;
}
}Interview Tip
Mention `unhandledrejection` — practical ops angle.
Common Mistake
Empty catch blocks that swallow failures silently.