🟨 JavaScript Intermediate

What is the difference between synchronous and asynchronous error handling?

Answer

Synchronous errors propagate up the call stack and are caught with try/catch at any point in the stack: try { JSON.parse("invalid"); } catch(e) { }. Uncaught sync errors crash the script and show in the console. Asynchronous errors (in callbacks, Promises, async functions) do NOT propagate through the synchronous call stack — they must be handled differently. For Promise-based code: every Promise chain should end with .catch(), or the entire chain should be awaited inside a try/catch. For async/await: wrap with try/catch — it converts rejected Promises to thrown errors. For traditional callbacks: Node.js uses the error-first convention — callback(err, result). A common mistake: wrapping a setTimeout or .then() callback in try/catch does not catch errors thrown inside the async callback — the try/catch has already completed by then. Always use await-based patterns with try/catch or Promise .catch().