constfn=async()=>{try{return1;}catch(e){
console.log('error', e);}};// 使用了顶级awaitconst data =awaitfn();
console.log('data', data);// data 1
Try 执行报错,函数执行返回undfined
constfn=async()=>{try{// 模拟报错thrownewError();return1;}catch(e){
console.log('error', e);}};const data =awaitfn();
console.log('data', data);// error Error// at fn (<anonymous>:4:15)// at <anonymous>:10:20// VM1563:11 data undefined
Try 执行报错,Catch有return
constfn=async()=>{try{// 模拟报错thrownewError();return1;}catch(e){
console.log('error', e);return2;}};const data =awaitfn();
console.log('data', data);// error Error// at fn (<anonymous>:4:15)// at <anonymous>:11:20// VM160819:12 data 2
Finally 中返回值会覆盖Try中的返回值
constfn=async()=>{try{return1;}catch(e){
console.log('error', e);}finally{return2;}};const data =awaitfn();
console.log('data', data);// data 2
Finally 中返回值会覆盖Catch中的返回值
constfn=async()=>{try{// 模拟报错thrownewError();return1;}catch(e){
console.log('error', e);return2;}finally{return3;}};const data =awaitfn();
console.log('data', data);// error Error// at fn (<anonymous>:4:15)// at <anonymous>:12:20// VM2590:13 data 3