用Generator封装Symbol中的iterator方法:

注意:Generator的function后必须写*

config:分别有3个txt文件,两个文件写路径,一个文件写要输出的内容

前置写法,node引入fs:

let fs = require('fs');

function read(path){
return new Promise((res, rej) => {
fs.readFile(path, 'utf-8', (err,data) => {
if(data){
res(data);
}else{
rej(err);
}
})
})
}

  

当前的迭代方法:
let obj = {
: 'a',
: 'b',
: 'c',
length:,
[Symbol.iterator]: function (){
let index = ;
let next = () => {
return {
value: this[index],
done: this.length == ++index
}
} return {
next
}
}
} console.log([...obj]);//(3) ["a", "b", "c"] for(let p of obj){
console.log(p);//a b c
} 改写后的方法:
let obj = {
: 'a',
: 'b',
: 'c',
length:,
[Symbol.iterator]: function* (){
let index = ;
while(index != this.length){
yield this[index++]
}
}
} console.log([...obj]);//(2) ["a", "b"] for(let p of obj){
console.log(p);//a b
}

用promise解决回调地狱问题与Generator方法对比

let fs = require('fs');

function read(path){
return new Promise((res, rej) => {
fs.readFile(path, 'utf-8', (err,data) => {
if(data){
res(data);
}else{
rej(err);
}
})
})
} read('./number.txt').then((value) => { return read(value);
}).then((value) => {
return read(value);
}).then((value) => {
console.log(value);
}) 以上缺点调用太多次then方法

那么我们用Generator方法解决一下回调地狱

function * readA() {

    let val1 = yield read('./number.txt');
let val2 = yield read(val1);
let val3 = yield read(val2); return val3;
}
//返回Generator对象
let oG = readA(); let {value, done} = oG.next(); value.then((val) => {
let {value, done} = oG.next(val);
value.then((val) => {
let {value, done} = oG.next(val);
value.then((val) => {
console.log(val)
})
})
}) 缺点:与回调地狱无区别

Generator优化:

注意:必须引入fs模块才能运行

function * readA() {

    let val1 = yield read('./number.txt');
let val2 = yield read(val1);
let val3 = yield read(val2); return val3;
} function Co(oIt){
return new Promise((res, rej) => {
let next = (data) => {
let {value, done} = oIt.next(data);
if(done != true){
value.then((val) => {
next(val);
})
}else{
res(value);
} }
next();
})
} Co(readA()).then((val) => {
console.log(val)
})

node有co模块,在npm下载使用,效果一样

ES6-Generator使用与改写的更多相关文章

  1. es6 generator函数的异步编程

    es6 generator函数,我们都知道asycn和await是generator函数的语法糖,那么genertaor怎么样才能实现asycn和await的功能呢? 1.thunk函数    将函数 ...

  2. ES6 Generator vs ES6 async/await

    ES6 Generator vs ES6 async/await next yield promise refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允 ...

  3. es6 generator函数

    es6 新增了Generator函数,一种异步编程的解决方案 回顾一下,es6 提供了新的遍历方法,for of ,适用于各种数据集合,统一了遍历操作,原生支持for of 集合的数据集合有.数组,字 ...

  4. ES6 Generator的应用场景

    一.基础知识 API文档 ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发布/订阅 Promise 对象 Generator 函数将 JavaScript 异步编程带入了一个 ...

  5. ES6 Generator async

    Generator 函数是 ES6 提供的一种异步编程解决方案 async 函数使得异步操作变得更加方便,是 Generator 函数的语法糖. js单线程的原因是:避免DOM渲染冲突! 更新:201 ...

  6. ES6 Generator 学习笔记一

    Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同. Generator 函数有多种理解角度.从语法上,首先可以把它理解成,Generator 函数是一个状态机 ...

  7. ES6 generator 基础

    参考文档 harmony:generators Generator是ES6的新特性,通过yield关键字,可以让函数的执行流挂起,那么便为改变执行流程提供了可能. 创建Generator functi ...

  8. ES6:Generator函数(1)

    Generator函数是ES6提供的一种异步编程解决方案.它会返回一个遍历器对象 function* helloWorldGenerator(){ yield “hello”; yield “worl ...

  9. es6 --- Generator 函数

    第一部分,ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路, ...

  10. ES6 Generator使用

    // generator介绍: function* hello() { console.log("hello world") } hello();//没有执行 // 直接调用hel ...

随机推荐

  1. Key ssd_300_vgg/block3_box/L2Normalization/gamma not found in checkpoint的解决方案

    在Tensorflow下使用SSD模型训练自己的数据集时,经过查找很多博客资料,已经成功训练出来了自己的模型,但就是在测试自己模型效果的时候,出现了如下错误. 2019-10-27 14:47:12. ...

  2. Codeforces Round #556 (Div. 1)

    Codeforces Round #556 (Div. 1) A. Prefix Sum Primes 给你一堆1,2,你可以任意排序,要求你输出的数列的前缀和中质数个数最大. 发现只有\(2\)是偶 ...

  3. Redis(四)Pub/Sub

    发布与订阅 Pub/Sub模式应该非常熟悉,在现实应用中被广泛的使用.如:微博中关注某个号,这个号有发新博时,关注的都会收到:github上watch了某个项目,当有issue时,就会发邮件. Red ...

  4. WPF 高级篇 MVVM 附加属性

    原文:WPF 高级篇 MVVM 附加属性 WPF 特性之一 附加属性 在本文里实现文本框内容的验证 public class TextBoxHelper:DependencyObject { publ ...

  5. C#安全类型转换基于convert

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; n ...

  6. mvc5中webapi的路由

    1.Global.asax中路由的注册 public class WebApiApplication : System.Web.HttpApplication { protected void App ...

  7. 【转载】Gradle学习 第十一章:使用Gradle命令行

    转载地址:http://ask.android-studio.org/?/article/94 This chapter introduces the basics of the Gradle com ...

  8. testNG结果入库

    一.使用IReporter接口 https://blog.csdn.net/oqqJohn1234567890/article/details/80900511  此文章中将结果数据打印成文本.

  9. Python Django Vue 项目创建

    环境安装忽略,可参考前面个篇幅介绍 1.创建项目 打开pycharm 终端,输入如下,创建项目 # 进入pycharm 项目目录下 cd pyWeb django-admin startproject ...

  10. python之pip安装软件包常用命令

    # pip版本号查询 pip -V # 安装软件包.格式:pip install 软件包名 pip install pygame # 安装指定版本号的软件包.格式:pip install 软件包==软 ...