// await 前的代码同步执行,await 后的代码异步执行
async function test(){
console.log(1)
await 2
console.log(2)
}
const p = test()
console.log(p)
image.png
-
promise.then
,即成功情况,对应await
-
promise.catch
,即异常情况,对应try...catch
async function test1(){
return 1
}
async function test2(){
return Promise.resolve(2)
}
const p1 = test1()
const p2 = test2()
console.log('p1:',p1)
console.log('p2:',p2)
async function test3(){
const p3 = Promise.resolve(3)
p3.then(data=>{
console.log('data-p3-then:',data)
})
const data = await p3
console.log('data-p3-await:',data)
}
test3()
async function test4(){
const data4 = await 4 //相当于 const data4 = await Promise.resovle(4)
console.log('data4:',data4)
}
test4()
async function test5(){
const data5 = await test1()
console.log('data5:',data5)
}
test5()
async function test6(){
const p6 = Promise.reject(6)
try{
const data6 = await p6
console.log('data6:',data6)
}catch(e){
console.log('err:',e)
}
}
test6()
image.png
网友评论