).catch((reason,data)=>{
console.log(“catch失败执行回调抛出原因”,reason)
})
then方法
======
then方法接收两个参数作为参数,第一个参数是Promise执行成功时的回调,第二个参数是Promise执行失败的回调,两个函数只会有一个被调用。
通过.then添加的回调函数,不论什么时候,都会被调用,而且可以添加多个回调函数,会一次按照顺序并且独立运行。
const p =new Promise((resolve,reject)=>{
resolve(“成功”)
})
p.then((res)=>{
console.log(res)//返回成功
},(err)=>{
console.log(err)
})
带有多个回调函数时
const p =new Promise((resolve,reject)=>{
resolve(1)
})
p.then((res1)=>{
console.log(‘res1’,res1) // 1
return res1 * 2;
}).then((res2)=>{
console.log(‘re 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 s2’,res2) //2
}).then((res3)