JavaScript Interview Question #151
What is the difference between synchronous exceptions and Promise rejections?
Performance, Browser APIs, Rendering & Modern Web Architecture Senior Advanced
Quick Interview Answer
A synchronous exception is thrown while ordinary JavaScript code is executing and can be caught by a surrounding synchronous try/catch. A Promise rejection represents failure of an asynchronous Promise operation and must be handled through await inside try/catch or with a rejection handler such as catch.
Detailed Explanation
A synchronous exception is thrown while ordinary JavaScript code is executing and can be caught by a surrounding synchronous try/catch. A Promise rejection represents failure of an asynchronous Promise operation and must be handled through await inside try/catch or with a rejection handler such as catch.
A try/catch around code that merely starts a Promise does not automatically catch a rejection that occurs later unless that Promise is awaited inside the try block.
Code Example
try {
const result = await loadData();
console.log(result);
} catch (error) {
console.error("Async failure:", error);
}
Promise.reject(new Error("Failed"))
.catch(error => console.error(error));