var a = new Promise(function (res, rej) {
rej(8);
});
a.then(
function (val) {
console.log(val + 2);
return val + 1;
},
function (val) {
console.log(val + 2);
return val + 1;
}
).catch(function (error) {
console.log(error);
});
a.finally(function () {
console.log("bb");
}).catch(function (d) {
console.log(d);
});
这个控制台输出什么?有没有谁能解释下为什么
而且上图和下图的输出竟然不一样,实在懵了
var a = new Promise(function (res, rej) {
rej(8);
}).then(
function (val) {
console.log(val + 2);
return val + 1;
},
function (val) {
console.log(val + 2);
return val + 1;
}
).catch(function (error) {
console.log(error);
});
a.finally(function () {
console.log("bb");
}).catch(function (d) {
console.log(d);
});
let a = new Promise((rew, rej) => {
rej(1);
})
a.then(
val => {
console.log(1);
}, val => {
console.log(2);//2
}).catch(res => {
console.log(3);
})
a.finally(res => {
console.log(4);//4
}).catch(res => {
console.log(5);//5
})
Promise有两种写法:
new Promise().then(f1,f2);
表示,成功时调用f1,失败时调用f2new Promise().then(f1).catch(f2);
表示,成功调用f1,失败调用f2new Promise().then(f1,f2).catch(f3);
你的写法,同时写的话,后面的catch里的回调方法不生效let a = new Promise((rew, rej) => {
rej(1);
}).then(
val => {
console.log(1);
}, val => {
console.log(2);//2
}).catch(res => {
console.log(3);
})
a.finally(res => {
console.log(4);//4
}).catch(res => {
console.log(5);
})
复制代码不要贴图片