标签:UNC throw ken const key syn pre 一个 return
//函数的三种定义 async function hello() { return "Hello" }; let hello = async function() { return "Hello" }; let hello = async () => { return "Hello" }; //使用 hello().then((value) => console.log(value)) hello().then(console.log) //简写
async函数返回值决定了返回的promise的状态
async function fn() { // throw 1 走catch // return Promise.reject(1) 走catch return 2 //走then return Promise.resolve(2) //走then return undefined //默认返回 走then }
function fn3() { return Promise.resolve(3) } const fn4 = async () => { console.log(1); //第一步 const res = await fn3() //await右边是promise对象,得到的结果是promise成功的value值 console.log(‘res: ‘, res); //第三步 } let res=fn4() console.log(2); //第二步 async function fn5(){ const data=await 30 //await右边不是promise,得到的结果是塔本身,即右边的值 console.log(‘data: ‘, data);//30 } fn5()
function fn3() { return Promise.reject(3) } const fn4 = async () => { console.log(1); //第一步 try { const res = await fn3() //await右边是promise对象,得到的结果是promise成功的value值 console.log(‘res: ‘, res); //第三步 } catch (error) { console.log(‘error: ‘, error);//3 promise失败的状态值 } } fn4()
标签:UNC throw ken const key syn pre 一个 return
原文地址:https://www.cnblogs.com/xiaoliziaaa/p/13196276.html