123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script src="./src/ajax.js"></script>
- </head>
- <body>
- <script>
- //async
- async function fn(){ //resolved
- return '111'
- }
- async function fn1(){ //rejected
- throw new Error('f')
- }
- async function fn2(){
- return new Promise((resolve,reject)=>{
- // resolve('success') //resolved
- reject('failed') //rejected
- })
- }
- //await
- //必须放在async函数中
- //await返回成功值
- //失败需要try catch捕获
- const p = new Promise((resolve,reject)=>{
- //resolve('success')
- reject('failed')
- })
- async function main(){
- try{
- let result = await p;
- console.log(result)
- }catch(e){
- console.log(e)
- }
- }
- main()
- //async和await结合
- //示例 ./src/index.js
- //async await 发送ajax示例
- // ./src/ajax.js
- //promise then
- sendAjax('https://api.apiopen.top/getJoke').then(value=>{
- //console.log(value)
- })
- //async await
- async function main1(){
- let result = await sendAjax('https://api.apiopen.top/getJoke')
- //console.log(result)
- }
- main1();
- /**
- * 对象方法扩展
- */
- const school = {
- name : 'hihi',
- cities:['beijing','shanghai'],
- sub:['php','java']
- }
- console.log(Object.keys(school))
- console.log(Object.values(school))
- console.log(Object.entries(school)) //把对象键和值转数组 [Array(2), Array(2), Array(2)]
- console.log(Object.getOwnPropertyDescriptors(school)) //对象属性描述
- </script>
- </body>
- </html>
|