12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <!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>
- </head>
- <body>
- <script>
- // const p = new Promise(function(resolve,reject){
- // setTimeout(function(){
- // let data = 'user data'
- // resolve(data)
- // },1000)
- // reject('err');
- // })
- // p.then(function(value){
- // console.log(value)
- // },function(reason){
- // console.log(reason)
- // })
- const p = new Promise((resolve,reject)=>{
- const xhr = new XMLHttpRequest()
- xhr.open("GET","https://api.apiopen.top/getJoke")
- xhr.send()
- xhr.onreadystatechange = function(){
- if(xhr.readyState === 4){
- if(xhr.status >= 200 && xhr.status < 300){
- resolve(xhr.response)
- }else{
- reject(xhr.status)
- }
- }
- }
- })
- p.then(function(value){
- console.log(value)
- },function(reason){
- console.log(reason)
- })
- //链式调用
-
- </script>
- </body>
- </html>
|