SOURCE

// async 确保了函数返回一个 promise,也会将非promise的值包装进去
async function getAsync(){
    return 1  // Promise{}
}

getAsync().then(res=>console.log(res,'-->返回了一个结果为1的promise'))  // 1

async function getAsync1(){
    return Promise.resolve(2) // Promise{}
} 
getAsync1().then(res=>console.log(res,'-->返回promise'))  // 2

// await 只在async函数内工作
// let value = await Promise  报错

// await不能在非async函数中
// function getAwait(){
//     await new Promise((resolve,reject)=>{
//         resolve('await不能在非async函数中')
//     })
// }


// await只能在async函数中
async function getAwait1(){
    let res =  await new Promise(function(resolve,reject){
        setTimeout( ()=>{
          resolve(777)
        },0)
    })

    console.log(res,'--->await返回的数据')
}

getAwait1()


// 可以使用 try catch来捕获使用async await的报错
 async function getTry(){
    try{

        let res = await new Promise((resolve,reject)=>{
            setTimeout(()=>{
               resolve('测试try catch捕获报错')
            },0)
        })

        console.log(res)

    }catch(err){
        console.log(err)
    }
}

getTry()
console 命令行工具 X clear

                    
>
console