1、什么是链式结构?
let p1=new Promise((resolve,reject)=>{
resolve('ok')
})
console.log(p1);
//像这样连续使用.then()方法的就被称为promise链
他能够解决回调地狱问题。
p1.then((value)=>{
console.log('111');
})
.then((value)=>{
console.log('222');
})
.then((value)=>{
console.log('333');
})
2、怎么去中断promise链呢?
其实很简单,只需要在这之中的任何一个.then()方法中我们返回一个新的promise对象就行。
let p1=new Promise((resolve,reject)=>{
resolve('ok')
})
console.log(p1);
p1.then((value)=>{
return new Promise((resolve,reject)=> { //这个新的promise对象不要更改状态,让他处于pending状态
// resolve(‘ok')不要更改失败还是成功的状态,否则会成为新的promise链
})
})
.then((value)=>{
console.log('222');
})
.then((value)=>{
console.log('333');
}).catch((error)=>{
console.log(error);
})
注意: Promise链中断的方式有且只有这一种