穿透和中断的理解 穿透是一级一级的传递下去,不是一步到达catch
new Promise((resolve, reject)=>{
// resolve(2),
reject(1)
}).then(
value=>{console.log('onResoled1()'+ value)},
reason=>{throw reason}
).then(
value=>{console.log('onResoled2()'+ value)},
reason=>{throw reason}//或return Promise.reject(reason) 不能reason=>throw reason 、reason=> reason
).catch(
reason=>{console.log('onReject1()'+ reason)//会执行下面的then 结果是onResoled3()undefined 如果想中断后面的then可以返回一个空的promise
return new Promise(()=>{}) //返回一个pending的promise,一直处于pending没有结果就会中断
}
).then(
value=>{console.log('onResoled3()'+ value)},
reason=>{console.log('onReject2()'+ reason)}
)
本文深入探讨了Promise链式调用的工作原理,包括resolve和reject如何穿透多个then方法,以及如何使用catch捕获错误。通过示例代码展示了如何在链式调用中控制流程,以及如何中断后续的then执行。
3309

被折叠的 条评论
为什么被折叠?



