function test(src){
return new Promise((success, fail)=>{
if(src == 1){
success(src);
}else if(src === 2){
fail(src);
}
});
}
var a = test(2);
a.then((src)=>{
console.log('success');
}, (src)=>{
console.log('fail');
}).then(()=>{
console.log(111)
},()=>{
console.log(222)
}).then(()=>{
console.log(333)
},()=>{
console.log(444)
})
输出结果:
"fail"
111
333
为什么不是:
"fail"
222
444
按照我的理解:
var a = test(2);
返回的Promise对象调用rejected函数,此时传递给第一个then的promise状态应该是rejected, 但是为什么调用的是resolved函数?
thx
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果顺序乱了,要promise有什么用
then返回的也是一个promise对象,若promise什么都没做的话,会默认执行它的resolve函数
then的第二个参数一用来捕获错误的,解决了就不抛到下一个then,没解决就要手动把错误继续抛下去.简单点就就相当于catch,你不在catch里面不抛错误,那默认就表示不用解决,错误没有了.