1.await函数必须要放在async函数当中
2.当await右面是promise对象的时候
<script> async function main() { let p=new Promise((resolve,reject)=>{ resolve('laozhichi') }) let result=await p console.log(result); } main() </script>
3.当async右面的非promise对象的时候
<script> async function main() { // let p=new Promise((resolve,reject)=>{ // resolve('laozhichi') // }) let result=await 10 console.log(result); } main() </script>
4.当promise是失败的时候,使用try{ }.catch( )函数
<script> async function main() { let p=new Promise((resolve,reject)=>{ reject('laozhichi') }) let result=await p console.log(result); } main() </script>
使用try{ }catch( )函数
<script> async function main() { let p=new Promise((resolve,reject)=>{ reject('laozhichi') }) try{ let result=await p }catch(e){ console.log(e); } } main() </script>