[ES6] 14. Generator -- 1. yield & next()
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()的更多相关文章
- es6编写generator报错
首先babel基础包(不安装额外东西)并不是支持完整的es6语言 自己写的如下代码 let generator = function* () { ; ,,]; ; }; var gen = gener ...
- ES6中Generator
ES6中Generator Generator是ES6一个很有意思的特性,也是不容易理解的特性.不同于let/const提供了块级作用域这样明显的目的,这玩意儿被搞出来到底是干嘛的? 首先我们需要明确 ...
- JS的ES6的Generator
JS的ES6的Generator 1.Generator函数的概念: ES6提供的解决异步编程的方案之一,现在已经不怎么用了被淘汰了. Generator函数是一个状态机,内部封装了不同状态的数据. ...
- Python generator和yield介绍
Python生成器(generator)并不是一个晦涩难懂的概念.相比于MetaClass和Closure等概念,其较为容易理解和掌握.但相对于程序结构:顺序.循环和分支而言其又不是特别的直观.无论学 ...
- generator 和 yield
yield 的使用 generator 生成器 yield 可以使生成器返回多次 我习惯于从表象推测,不喜欢官方文档,写的字都认识,结果变成句子之后,就一句都看不懂 所以先举一个例子来看一下这个东西怎 ...
- 14 Generator
Generator 就是可以返回多个结果,也是支持 Iterator 接口. function* helloWorldGenerator() { yield 'hello'; yield 'world ...
- es6之Generator
1.Generator函数其实是一个封装了多个内部状态的状态机,执行它会返回一个遍历器对象,然后可以依次遍历Generator中的每一个状态,也就是分段执行,yield是暂停执行的标记,next恢复执 ...
- ES6的generator函数
generator是什么? generator是ES6提供的一种异步编程解决方案,在语法上,可以把它理解为一个状态机,内部封装了多种状态.执行generator,会生成返回一个遍历器对象.返回的遍历器 ...
- [Advanced Python] 14 - "Generator": calculating prime
高性能编程 几个核心问题 • 生成器是怎样节约内存的?• 使用生成器的最佳时机是什么?• 我如何使用 itertools 来创建复杂的生成器工作流?• 延迟估值何时有益,何时无益? From: htt ...
随机推荐
- MXML的一些基本语法
以下内容是一个视频的学习笔记<Flex4视频教程>,所以,先关记录也是以现在的Flash Builder为基础. <fx:Script/> 是脚本文件的声明 var代表数值, ...
- 【C traps and pit falls】阅读笔记
已经是第几遍读C 陷阱与缺陷了,某种意义上,这不是一本常读常新的书,大概是因为读一遍过后就记住了. 一.编译器在将程序分解成符号的时候,使用的是大嘴法. 二.使用不对称边界有很多好处. 三.缓冲输出与 ...
- Spring Batch的事务– Part 3: 略过和重试
原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...
- Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)
First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...
- Strider-test 相关配置
package.json { "name": "test-node", "version": "0.0.0", &quo ...
- 第三百五十八天 how can I 坚持
万事要有度,不要话唠,也不能不说,把握好分寸,今天貌似又说多了. 加了天班,理了个发,还有老爸明天来北京. 还有同学聚会没去,还有金龙让去吃鱼,没去. 还有.小米视频通话还行,能远程控制桌面, 还有, ...
- 第三百零八至三百二十天 how can I 坚持
十三天..2月4号至2月16号,好快,假期还没开始就结束了.一一回忆下. 2月4号,腊月二十六,最后一天上班,没多大事,好像是玩了一天,东月回家,貌似路上好折腾,晚上D401,和她聊了一路,也聊了好多 ...
- E:Package 'Vim' has no installation candidate问题解决
问题描述: root@zhouls-virtual-machine:~# apt-get install vimReading package lists... DoneBuilding depend ...
- Advanced Scene Processing
[How a Scene Processes Frames of Animation] In the traditional view system, the contents of a view a ...
- 实时监控MySql状态
大多网站的性能瓶颈都会出在数据库上,所以想把Mysql监控起来,就搜索了下相关资料. 后来和同事讨论了下cacti和nagios有些老套和过时,graphite比较时尚,然后就搜了下相关的资料,最后搞 ...