index.html 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="./src/ajax.js"></script>
  9. </head>
  10. <body>
  11. <script>
  12. //async
  13. async function fn(){ //resolved
  14. return '111'
  15. }
  16. async function fn1(){ //rejected
  17. throw new Error('f')
  18. }
  19. async function fn2(){
  20. return new Promise((resolve,reject)=>{
  21. // resolve('success') //resolved
  22. reject('failed') //rejected
  23. })
  24. }
  25. //await
  26. //必须放在async函数中
  27. //await返回成功值
  28. //失败需要try catch捕获
  29. const p = new Promise((resolve,reject)=>{
  30. //resolve('success')
  31. reject('failed')
  32. })
  33. async function main(){
  34. try{
  35. let result = await p;
  36. console.log(result)
  37. }catch(e){
  38. console.log(e)
  39. }
  40. }
  41. main()
  42. //async和await结合
  43. //示例 ./src/index.js
  44. //async await 发送ajax示例
  45. // ./src/ajax.js
  46. //promise then
  47. sendAjax('https://api.apiopen.top/getJoke').then(value=>{
  48. //console.log(value)
  49. })
  50. //async await
  51. async function main1(){
  52. let result = await sendAjax('https://api.apiopen.top/getJoke')
  53. //console.log(result)
  54. }
  55. main1();
  56. /**
  57. * 对象方法扩展
  58. */
  59. const school = {
  60. name : 'hihi',
  61. cities:['beijing','shanghai'],
  62. sub:['php','java']
  63. }
  64. console.log(Object.keys(school))
  65. console.log(Object.values(school))
  66. console.log(Object.entries(school)) //把对象键和值转数组 [Array(2), Array(2), Array(2)]
  67. console.log(Object.getOwnPropertyDescriptors(school)) //对象属性描述
  68. </script>
  69. </body>
  70. </html>