1.js的执行顺序
1.从前到后,一行一行的执行
2.如果某一行代码报错,则停止后面代码的执行
3.先把同步代码执行完,再执行异步
2.then和catch改变状态
then正常返回resolved,里面有报错返回rejected
catch正常返回resolved,里面有报错返回rejected
Promise.resolved().then(()=>{
console.log(1)
}).catch(()=>{
console.log(2)
}).then(()=>{
console.log(3)
})
//1 3
Promise.resolved().then(()=>{
console.log(1)
throw new Error('error1')
}).catch(()=>{
console.log(2)
}).then(()=>{
console.log(3)
})
//1 2 3
Promise.resolved().then(()=>{
console.log(1);
throw new Error('error1')
}).catch(()=>{
console.log(2)
}).catch(()=>{
console.log(3)
})
//1 2
3.async与promise的关系
async function(){
const p1=Promise.resolve(200);
const data=await p1;//await 相当于promise.then
console.log('data',data);
}