JavaScript · Senior · Question 4 of 75

Why will the promise below never reject?

new Promise((resolve, reject) => {
  setTimeout(() => { throw new Error("boom"); }, 0);
});

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.

Further reading

Problem with a question

JavaScript · Senior · question 4

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz