Generators in ECMAscript 6 are first-class coroutines that produce encapsulated suspended execution (暂停执行) contexts.

Yield values and iterate over them until no more values exist in the generator.

You use generator by adding a * after function keyword:

function* greeting(){
console.log(`You should call next()`);
} greeting(); // Nothing happens

When you call greeting(), you will find nothing happens.

For better understaning what happened, let console out what greeting() returns:

function* greeting(){
console.log(`You called 'next()'`);
yield "hello";
} let greeter = greeting();
console.log(greeter); //{next: [Function], throw: [Function]}

What you can see is greeter is actually an object. It didn't invoke:

console.log(`You should call next()`);  

Call the next():

function* greeting(){
console.log(`You called 'next()'`);
} let greeter = greeting();
let next = greeter.next();
console.log(next); // {value: undefined, done: true}

When calling the next(),

 console.log(`You called 'next()'`);

are invoked, and the value of the next is undefined and done = true.

Why value is undefined?:

Because we didn't call any yield statment.

Why done is ture?:

Because it has gone thought all yield statmens (0 = gone thought)

So let us add yield statment:

function* greeting(){
console.log(`You called 'next()'`);
yield "hello";
} let greeter = greeting();
let next = greeter.next();
console.log(next); // {value: "hello", done: false}

From the output we see, value has been chaned to "hello", but done is false.

So why done is false?:

Because we should call next() to make sure we have gone thought all the yield statmenets.

So let us add next() again:

function* greeting(){
console.log(`You called 'next()'`);
yield "hello";
} let greeter = greeting();
console.log(greeter); //{next: [Function], throw: [Function]}
let next = greeter.next();
console.log(next); // {value: "hello", done: false}
let done = greeter.next();
console.log(done); // {value: "hello", done: true}

Example:

function* greeting(){
console.log(`Generators are "lazy"`);
yield "How";
console.log(`I'm not called until the second next`);
yield "are";
console.log(`Call me before "you"?`);
yield "you";
console.log(`Called when "done"`);
} let greeter = greeting();
let next = greeter.next(); // Generators are "lazy" {value: "How", done: false}

You can see that it stop at the first yield. It won't go to next until you call next();

 Meaning that you can put stuff in here (after the first yield) that's not created until you explicitly need it.

If add next() three more times, all statments will be output.

[ES6] 14. Generator -- 1. yield & next()的更多相关文章

  1. es6编写generator报错

    首先babel基础包(不安装额外东西)并不是支持完整的es6语言 自己写的如下代码 let generator = function* () { ; ,,]; ; }; var gen = gener ...

  2. ES6中Generator

    ES6中Generator Generator是ES6一个很有意思的特性,也是不容易理解的特性.不同于let/const提供了块级作用域这样明显的目的,这玩意儿被搞出来到底是干嘛的? 首先我们需要明确 ...

  3. JS的ES6的Generator

    JS的ES6的Generator 1.Generator函数的概念: ES6提供的解决异步编程的方案之一,现在已经不怎么用了被淘汰了. Generator函数是一个状态机,内部封装了不同状态的数据. ...

  4. Python generator和yield介绍

    Python生成器(generator)并不是一个晦涩难懂的概念.相比于MetaClass和Closure等概念,其较为容易理解和掌握.但相对于程序结构:顺序.循环和分支而言其又不是特别的直观.无论学 ...

  5. generator 和 yield

    yield 的使用 generator 生成器 yield 可以使生成器返回多次 我习惯于从表象推测,不喜欢官方文档,写的字都认识,结果变成句子之后,就一句都看不懂 所以先举一个例子来看一下这个东西怎 ...

  6. 14 Generator

    Generator 就是可以返回多个结果,也是支持 Iterator 接口. function* helloWorldGenerator() { yield 'hello'; yield 'world ...

  7. es6之Generator

    1.Generator函数其实是一个封装了多个内部状态的状态机,执行它会返回一个遍历器对象,然后可以依次遍历Generator中的每一个状态,也就是分段执行,yield是暂停执行的标记,next恢复执 ...

  8. ES6的generator函数

    generator是什么? generator是ES6提供的一种异步编程解决方案,在语法上,可以把它理解为一个状态机,内部封装了多种状态.执行generator,会生成返回一个遍历器对象.返回的遍历器 ...

  9. [Advanced Python] 14 - "Generator": calculating prime

    高性能编程 几个核心问题 • 生成器是怎样节约内存的?• 使用生成器的最佳时机是什么?• 我如何使用 itertools 来创建复杂的生成器工作流?• 延迟估值何时有益,何时无益? From: htt ...

随机推荐

  1. 2014 IGF 评选(转)

    前两天受邀去上海参加今年的独立游戏节评选,准确说是亚洲及太平洋地区的 IGF . 居然有接近 400 个参选游戏是让我事前没有想到的,尤其是在学生组还发现了不少好作品是个惊喜. 评审用了整整两天时间, ...

  2. OPENGL学习笔记整理(五):着色语言

    有些事情本身就是十分奇怪的.在传统上,图形硬件的设计目的是用于快速执行相同的硬编译指令集.不同的计算步骤可以被跳过,参数可以被调整,但计算本身确实固定不变的.然而,随着技术的发展,却越来越变得可以编程 ...

  3. bzoj1833 digit

    这道题其实挺水,只是写的时候需要想清楚.我的方法是: 1.将[a,b]转化为[0,b+1)-[0,a) 2.预处理出非0的v在区间[0,10^p)出现次数以及0在区间[0,10^p)出现数 3.将一个 ...

  4. (转载)OC学习篇之---协议的概念和用法

    在前一篇文章中我们介绍了OC中类的延展,这一篇文章我们在来看一下OC中协议的概念以及用法,协议也是OC中的一个重点,Foundation框架以及我们后面在写代码都会用到. OC中的协议就是相当于Jav ...

  5. jqueryMobile

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  6. Camel In Action 阅读笔记 第一部分概述 + 第一章概述 认识Camel

    第一部分: 最开始的一小步 Apache Camel 是一个开源集成框架,其目的是让系统集成变得更加简便,在本书的第一章中,我们会为您介绍它并向您展示它是如何在大型企业应用中做好集成工作.您也会了解到 ...

  7. MySQL 5.6 警告信息 command line interface can be insecure 修复

    在命令行输入密码,就会提示这些安全警告信息. Warning: Using a password on the command line interface can be insecure.   注: ...

  8. 《Linux设备驱动程序》 笔记2

    驱动代码hello.c #include <linux/init.h> #include <linux/module.h> static int hello_init(void ...

  9. gem openssl 出错

    Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources 1. ...

  10. 转】启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/5288399.html   感谢! 一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务 ...