1、定义和使用

function *gen() {
return 'first generator';
} // 有点类似类的实例化过程
let generatorResult = gen() // 核心方法next
generatorResult.next() // {value: "first generator", done: true} // Generator 如同一个序列:一旦序列中的值被消费,你就不能再次消费它。
generatorResult.next() // undefined

2、next() / value

function *gen() {
yield 'fitst';
yield 'second';
yield 'third';
} let genResult = gen();
genResult.next().value // first
genResult.next().value // second
genResult.next().value // third

3、for ... of  ...

function *gen() {
yield 'fitst';
yield 'second';
yield 'third';
} for (const value of gen()) {
console.log(value);
} // fitst
// second
// third

4、next() 往 generator 中赋值

function *gen() {
var firstname = yield;
var secondname = yield;
console.log(firstname + secondname);
} var genResult = gen()
genResult.next()
genResult.next('Mr. ')
genResult.next('Right') // Mr. Right // 解释一下,由于yield可以理解为暂停器。
// 当第一次调用 next 时,代码将返回并且暂停于此: var firstname = yield;
// 有趣的事情发生在第二次调用 next 时: genResult.next('Mr. ')。
// 此时我们向 next 调用传入了值!Generator将从上一次暂停中恢复,并且 yield将被 "Mr. " 替换。因此firstname的值变成'Mr. '
// 然后继续执行,而又遇到yield处再次暂停: genResult.next('Right')
// 第三次调用 next: genResult.next('Right')
// 同前面一样,传入的 'Right' 将替换 yield,并在赋值完后继续执行。
// 由于没有yield了。所以正常执行了: genResult.next('Right') // Mr. Right

5、使用gen异步操作

let gen;

let getDataOne = () => {
setTimeout(function () {
gen.next('one')
}, 1000);
} let getDataTwo = () => {
setTimeout(function () {
gen.next('two')
}, 1000);
} function *main() {
let dataone = yield getDataOne();
let datatwo = yield getDataTwo();
console.log(dataone, datatwo);
} gen = main();
gen.next(); // {value: undefined, done: false}
// 1秒后输出: one two

es6 generator 基础知识的更多相关文章

  1. ES6 generator 基础

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

  2. es2015(es6)基础知识整理(更新中...)

    1.let let可以声明块级作用域变量 'use strict'; if (true) { let app = 'apple'; } console.log(app); //外面是访问不到app的 ...

  3. ES6的基础知识总结

    一. ES6 ES6中定义变量使用 let/const let 使用let定义的变量不能进行"变量提升" 同一个作用域中,let不能重复定义相同的变量名 使用var在全局作用域中定 ...

  4. ES6的基础知识(一)

    1.ECMAScript 6.0(以下简称ES6). 2.ECMAScript 和 JavaScript 的关系是,前者是后者的规格,后者是前者的其中一种实现. 3.对ES6支持的浏览器:超过 90% ...

  5. ES6 Generator的应用场景

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

  6. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  7. 性能测试学习 第九课--LR12中controller基础知识

    1.设计手工场景,理解集合点的策略 2.添加load generator 一.controller基础知识 1.controller的原理 通过场景设计来模拟用户的真实操作并调用vugen中的脚本,然 ...

  8. javascript基础知识笔记-自用

    笔记内容根据个人基础知识不足不明白之处做的记录.主要看的:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript 1.变量,变量的名字又叫标识符 ...

  9. React Native 入门基础知识总结

    中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...

随机推荐

  1. 数字签名算法(C#)

    public static string GetSHA1Method(string strSource) { string strResult = ""; //Create Sys ...

  2. dubbo+maven多模块项目单元测试

    基本上就是记录各种报错的解决办法.基本上就是将散落在项目各个模块中的配置文件复制到测试模块中. 目录结构: ——src ——java ——test ——java ——DaoTest.java ——re ...

  3. 从头说catalan数及笔试面试里那些相关的问题 (转)

    作者:寒小阳 时间:2013年9月. 出处:http://blog.csdn.net/han_xiaoyang/article/details/11938973. 声明:版权所有,转载请注明出处,谢谢 ...

  4. Sql server management studio: cannot find one or more components

      Install VS2010 SHELL 独立组件 https://www.microsoft.com/en-US/download/details.aspx?id=1366 运行安装程序,rep ...

  5. nginx学习笔记(四)-----日志切割脚本及定时任务

    一.日志切割脚本 #!/bin/sh #nginx目录 BASE_DIR=/usr/local/nginx #生成的日志 BASE_FILE_NAME=jonychen.access.log CURR ...

  6. The platform of the target `Pods` (iOS 4.3) is not compatible 错误

    一:使用 cocoaPod错误 The platform of the target `Pods` (iOS 4.3) is not compatible with `AFNetworking (1. ...

  7. 调整UIPickerView高度

    Advantages: Makes setFrame of UIPickerView behave like it should No transform code within your UIVie ...

  8. uni - 条件渲染

    vue官方文档和uni官方同步:https://cn.vuejs.org/v2/guide/conditional.html 1.多次切换建议使用v-show(始终保存在BOM) 2.因为if是惰性判 ...

  9. plsql 常用快捷键(自动替换)

      plsql 常用快捷键 CreateTime--2018年4月23日17:33:05 Author:Marydon 说明:这里的快捷键,不同于以往的快捷键,输入指定字符,按快捷键,可以自动替换成你 ...

  10. 整理两个JVM博客集合,空闲时候可以看

    纯洁的微笑写的:https://www.cnblogs.com/ityouknow/p/5614961.html 集合:http://www.cnblogs.com/ityouknow/categor ...