博客列表 >fetch / async / await

fetch / async / await

手机用户311660634
手机用户311660634原创
2022年11月17日 05:09:59442浏览
  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>fetch / async / await </title>
  8. </head>
  9. <body>
  10. <script>
  11. // fetch
  12. fetch('https://jsonplaceholder.typicode.com/todos/9')
  13. .then((Response) => Response.json())
  14. .then((json => console.log(json)))
  15. // async await function 前面假async,使它变成一个异步函数
  16. async function getUser(){
  17. let url = 'https://jsonplaceholder.typicode.com/todos/8'
  18. const response = await fetch(url)
  19. const result = await response.json()
  20. console.log(result)
  21. }
  22. </script>
  23. </body>
  24. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议