DESKTOP-MK04A0R\chuck %!s(int64=3) %!d(string=hai) anos
pai
achega
74bfd7939c
Modificáronse 7 ficheiros con 130 adicións e 0 borrados
  1. 14 0
      17/ajax.js
  2. 47 0
      17/index.html
  3. 63 0
      17/index.js
  4. 3 0
      17/resources/text.md
  5. 1 0
      17/resources/text1.md
  6. 1 0
      17/resources/text2.md
  7. 1 0
      README.md

+ 14 - 0
17/ajax.js

@@ -0,0 +1,14 @@
+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)
+            }
+        }
+    }
+})

+ 47 - 0
17/index.html

@@ -0,0 +1,47 @@
+<!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>

+ 63 - 0
17/index.js

@@ -0,0 +1,63 @@
+//读取text.md
+const fs = require('fs')
+
+// fs.readFile('./resources/text.md',(err,data)=>{
+//     if(err) throw err
+//     console.log(data.toString())
+// })
+
+//使用promise封装
+const p = new Promise(function(resolve,reject){
+    fs.readFile('./resources/text.md',(err,data)=>{
+        if(err) reject('失败')
+        resolve(data)
+    })
+})
+// p.then(function(value){
+//     console.log(value.toString())
+// },function(err){
+//     console.log(err)
+// })
+
+// p
+// .then(function(value){
+//     console.log(value.toString())
+// })
+// .catch(function(err){
+//     console.log(err)
+// })
+
+//多个文件内容读取 -- 回调地狱
+// fs.readFile('./resources/text.md',(err,data1)=>{
+//     fs.readFile('./resources/text1.md',(err,data2)=>{
+//         fs.readFile('./resources/text2.md',(err,data3)=>{
+//             let result = data1 + data2 + data3
+//             console.log(result)
+//         })
+//     })
+// })
+
+//使用promise实现
+const p1 = new Promise((resolve,reject)=>{
+    fs.readFile('./resources/text.md',(err,data)=>{
+        resolve(data)
+    })
+})
+
+p1.then(value=>{
+    return new Promise((resolve,reject)=>{
+        fs.readFile('./resources/text1.md',(err,data)=>{
+            resolve([value,data])
+        })
+    })
+}).then(value =>{
+    return new Promise((resolve,reject)=>{
+        fs.readFile('./resources/text2.md',(err,data)=>{
+            value.push(data)
+            resolve(value)
+        })
+    })
+}).then(value=>{
+    console.log(value.join('\r\n'))
+})
+

+ 3 - 0
17/resources/text.md

@@ -0,0 +1,3 @@
+成功乃失败之母
+你好 世界
+我名由我不由天

+ 1 - 0
17/resources/text1.md

@@ -0,0 +1 @@
+test1

+ 1 - 0
17/resources/text2.md

@@ -0,0 +1 @@
+test2

+ 1 - 0
README.md

@@ -15,6 +15,7 @@
 * 14 迭代器
 * 15 生成器函数
 * 16 生成器函数实例
+* 17 promise
 
 ## 运行说明
     npm i