then
1. then的参数必须是方法,不是方法的话这个then会被略过,不执行
2.then(resolve, error),该写法中,error方法捕捉不到resolve方法里的错误。只能通过then().catch(err)这种形式,用catch来捕获then里面的错误
catch
如下,f1在return propmise之前就抛出了同步错误,这个时候还没跑进promise里,后面的catch根本没执行到
function f1(){
throw new Error('error')
return new Promise(resolve =>{
setTimeout(() => {
resolve(1)
}, 1000)
})
}
function f(x){
return f1.then(()=>{}).catch(()=>{})
}
修改方法为利用Promise.resolve:
function f(x){
return Promise.resolve(x).then(f1).then().catch()
}