case 1:
let promise = new Promise((resolve,reject)=>{
reject(new Error('Test'))
}).catch(e=>{
console.log(e)
})
console.log(promise)
//Promise {<fulfilled>: undefined}
//__proto__: Promise
//[[PromiseState]]: "fulfilled"
//[[PromiseResult]]: undefined
解析:
//case1 代码等同于
let promise = new Promise((resolve,reject)=>{
reject(new Error('Test')) //此处promise 处于rejected状态
})
promise = promise.catch(e=>{
//`promise.catch` 会返回一个新的promise,
//catch捕捉上一个promise错误,pending->fulfilled
console.log(e) //
})
console.log(promise) //所以最后返回的是fulfilled状态
case 2:
let promise = new Promise((resolve,reject)=>{
reject(new Error('Test')) //pending -> rejected
})
promise.catch(e=>{
//与case1的区别是,此处未对promise做重新赋值。
//另外注意:promise内部的状态一旦确定,就无法改变
console.log(e)
})
console.log(promise)
//Promise {<rejected>: Error: Test
// at <anonymous>:2:9
// at new Promise (<anonymous>)
// at <anonymous>:1:15}
总结: Promise链式调用有时并非想象的结果。