promise then catch执行问题
1.
Promise.resolve()
.then(res => {
console.log(1);
})
.catch(err => {
console.log(2);
})
.then(res => {
console.log(3);
})
2.
Promise.resolve()
.then(res => {
console.log(1);
throw new Error('error')
// 这里抛出错误状态变为rejected,被下面catch捕获
})
.catch(err => {
console.log(2);
// 这里正常,状态为resolved,被下面then捕获
})
.then(res => {
console.log(3);
})
3.
Promise.resolve()
.then(res => {
console.log(1);
throw new Error('error')
// 这里抛出错误状态变为rejected,被下面catch捕获
})
.catch(err => {
console.log(2);
// 这里正常,状态为resolved,下面没有then。不会往下执行
})
.catch(res => {
console.log(3);
})
4.
Promise.resolve()
.then(res => {
console.log(1);
throw new Error('error')
// 这里抛出错误状态变为rejected,被下面catch捕获
})
.catch(err => {
console.log(2);
// 这里正常,状态为resolved,下面有then。会往下执行
})
.catch(res => {
console.log(3);
})
.then(res => {
console.log(4)
})
答案:
1 31 2 31 21 2 4