为什么下面的代码无法捕获错误?'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')
})
为什么会发生这种情况?请帮忙,谢谢!