我正在研究Promise()
构造函数,我注意到了一些适合我的unexpected.
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?
输出:
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>)
预期输出(I expect this output as the 100 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>)
构造函数的executor
据说运行synchronously,因此:
如果它应该是stop the execution of the script after each exception(在console.log(what();
之后),为什么它会记录continues
?
我知道我应该用catch()
来表示拒绝promise ,但这不是问题的重点.