JavaScript · Senior · Question 4 of 75
Why will the promise below never reject?
new Promise((resolve, reject) => {
setTimeout(() => { throw new Error("boom"); }, 0);
});- ✓Correct answer: The exception in the timer is thrown outside the executor and isn't tied to the promise
- 2
setTimeoutinside a promise automatically swallows any exceptions - 3The promise will reject, but only after
.catch()is called explicitly
Explanation
Only synchronous exceptions in the executor automatically become rejections. An error in a timer callback happens later, in a separate task: it becomes a global unhandled error, and the promise stays pending forever. You need to call reject explicitly.