• js异步解决方案-基于nodejs


    详解js异步的解决方案:promise,generator,async/await

     

    1. es6之前解决方案:
      1. 回调函数
        //引入fs模块
        var fs = require("fs");
        fs.readFile("test.txt",function(err,res) {
            //执行回调函数
            console.log(res.toString());
        })
      2. 发布订阅模式(先有人订阅事件,事件完成后发布者通知订阅者)
        const fs = require("fs");  // nodejs 文件模块(用于操作文件的模块)
        const event = require("events");  // nodejs 的事件模块
        const readFileEvent = new event.EventEmitter();  //创建事件对象
        // 监听事件,订阅
        readFileEvent.on("read1",(data) => {
            let file1Data = data;
            // 根据读取到的test.txt文件的内容为地址,读取test2.txt文件的内容。
            fs.readFile(file1Data.toString(),(err,data) => {
                readFileEvent.emit("read2",data)
            })
        })
        // 绑定文件test2.txt读取成功时的处理函数
        readFileEvent.on("read2",(data) => {
            console.log(data.toString());
        })
         
        // 读取test.txt的内容
        fs.readFile("./test.txt",(err,data) => {
            //触发事件,发布
            readFileEvent.emit("read1",data)
        })

        问题:回调函数嵌套,代码结构混乱

    2. es6版本
      1. promise
        const fs = require("fs");  // nodejs 文件模块(用于操作文件的模块)
        //promise有两个参数
        new Promise((resolve,reject)=>{
            fs.readFile("./test.txt",(err,data) => {
                let result = data.toString();
                resolve(result);
            })
        }).then((res)=>{
            console.log(res)
        },()=>{})

           也可以直接使用node8的util工具包可以将fs模块转化

    const fs = require("fs");  // nodejs 文件模块(用于操作文件的模块)
    const util = require("util"); //await后面要是promise,所以使用util包转化为promise
    const readAsync = util.promisify(fs.readFile);
    readAsync("test.txt")
    .then((res)=>{
        console.log(res.toString())
    })
      1. Generator
        const fs = require("fs");  // nodejs 文件模块(用于操作文件的模块)
        let t = readfile();
        function* readfile(){
            let res = yield fs.readFile("test.txt",(err,data)=>{ t.next(data.toString()) })
            res = yield fs.readFile(res,(err,data)=>{  t.next(data.toString()) })
            res = yield fs.readFile(res,(err,data)=>{  t.next(data.toString()) })
            console.log(res);
        }
        t.next();

    1. es7------async/await写法

      const fs = require("fs");  // nodejs 文件模块(用于操作文件的模块)
      const util = require("util"); //await后面要是promise,所以使用util包转化为promise
      const readAsync = util.promisify(fs.readFile);
      async function readThreeFile() {
          var res = ""
          res = await readAsync("test.txt");
          res = await readAsync(res.toString());
          res = await readAsync(res.toString());
          console.log(res.toString());
      }
      readThreeFile();
  • 相关阅读:
    Java方向如何准备技术面试答案(汇总版)
    HTTP与HTTPS的区别
    MyBatisPlus环境下使用MyBatis的配置类
    idea 插件的使用 进阶篇(个人收集使用中的)
    Leveldb实现原理
    浅析 Bigtable 和 LevelDB 的实现
    IKAnalyzer进行中文分词和去停用词
    Elasticsearch之中文分词器插件es-ik的自定义词库
    使用Java High Level REST Client操作elasticsearch
    任正非:坚持逐渐辞退低绩效员工
  • 原文地址:https://www.cnblogs.com/longlongdan/p/10538484.html
Copyright © 2020-2023  润新知