[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 ...
随机推荐
- python中的静态方法和类方法
在python中,各种方法的定义如下所示: class MyClass(object): #在类中定义普通方法,在定义普通方法的时候,必须添加self def foo(self,x): print & ...
- Red Hat Linux认证
想系统的学习一下Linux,了解了一些关于Red Hat Linux认证的信息.整理如下. 当前比较常见的是RHCE认证,即Red Hat Certified Engineer.最高级别的是RHCA ...
- 哈希(Hash)与加密(Encrypt)的基本原理、区别及工程应用
0.摘要 今天看到吉日嘎拉的一篇关于管理软件中信息加密和安全的文章,感觉非常有实际意义.文中作者从实践经验出发,讨论了信息管理软件中如何通过哈希和加密进行数据保护.但是从文章评论中也可以看出很多朋友对 ...
- Strider SSH Deploy配置
登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令
- Strider 持续集成(gitlab)
Strider安装后运行: Mac: strider Ubuntu: bin/strider 本地运行时浏览器访问: http://127.0.0.1:3000 其他服务器:服务器地址 + 端口号(3 ...
- js运动 多物体运动含Json 但是里面数值不一样
<!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...
- 转自 处理老版PIL 到 pillow
帮新同事部署开发环境, 由于项目代码里用到了PIL库处理图片, 导致一些图片在浏览器中无法正常显示. 几番折腾, 解决了问题, 这里记录一下报的问题, 及解决方法: 1. python版本不对, 6 ...
- 第二百二十九天 how can I 坚持
百度-让人更容易的获取信息,腾讯-让人更方便的交流,阿里-让人更方便的消费,每个公司都有自己的使命,每个公司的使命都是围绕着人. 创新-其实应该是在每个人的内心深处都或多或少有一些新的想法,但是什么是 ...
- Spark RDD概念学习系列之RDD的缺点(二)
RDD的缺点? RDD是Spark最基本也是最根本的数据抽象,它具备像MapReduce等数据流模型的容错性,并且允许开发人员在大型集群上执行基于内存的计算. 为了有效地实现容错,(详细见ht ...
- 使用Map List 封装json数据
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...