index.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. </head>
  9. <body>
  10. <script>
  11. // const p = new Promise(function(resolve,reject){
  12. // setTimeout(function(){
  13. // let data = 'user data'
  14. // resolve(data)
  15. // },1000)
  16. // reject('err');
  17. // })
  18. // p.then(function(value){
  19. // console.log(value)
  20. // },function(reason){
  21. // console.log(reason)
  22. // })
  23. const p = new Promise((resolve,reject)=>{
  24. const xhr = new XMLHttpRequest()
  25. xhr.open("GET","https://api.apiopen.top/getJoke")
  26. xhr.send()
  27. xhr.onreadystatechange = function(){
  28. if(xhr.readyState === 4){
  29. if(xhr.status >= 200 && xhr.status < 300){
  30. resolve(xhr.response)
  31. }else{
  32. reject(xhr.status)
  33. }
  34. }
  35. }
  36. })
  37. p.then(function(value){
  38. console.log(value)
  39. },function(reason){
  40. console.log(reason)
  41. })
  42. //链式调用
  43. </script>
  44. </body>
  45. </html>