I'm studying the Promise()
constructor, and I noticed something unexpected for my me.
console.log('first');
const promise1 = new Promise((resolve, reject) => {
console.log('inside executor');
let what = 1
console.log(what());
console.log('not reached');
resolve('Hi Guys!');
});
console.log('continues'); // why does it continue?
Output:
first
inside executor
continues // whaaaaaaaaaaaaaaaaaaaaaaaat?????????
index.js:5 Uncaught (in promise) TypeError: what is not a function
at index.js:5:15
at new Promise (<anonymous>)
Expected output (I expect this output as the executor
runs synchronously):
first
inside executor
index.js:5 Uncaught (in promise) TypeError: what is not a function
at index.js:5:15
at new Promise (<anonymous>)
The executor
the constructor is said to run synchronously, so:
why does it log continues
if it should stop the execution of the script after each exception (after console.log(what();
)?
I understand that I should use e.g. catch()
for the promise rejection but that is not the main point of the question.