<分区>
为什么下面的代码无法捕捉到错误? 'catch' 不起作用,错误抛出并退出。我在 nodeJs 8.9 上运行代码
new Promise(function(resolve,reject){
console.log('construct a promise...')
setTimeout(() => {
throw new Error('async operation failure!')
},1000)
})
.then(() => {
//never happen
console.log('happen?wrong!!!')
})
.catch(e => {
console.warn('execute promise failure! catch it')
})
如果删除 setTimeout,'catch' 起作用
new Promise(function(resolve,reject){
console.log('construct a promise...')
throw new Error('async operation failure!')
})
.then(() => {
//never happen
console.log('happen?wrong!!!')
})
.catch(e => {
console.warn('execute promise failure! catch it')
})
为什么会这样? 请帮忙,谢谢!