[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 ...
随机推荐
- 关于Activity的少许细节
1. 对活动应用样式和主题 2. 隐藏活动标题 3. 显示对话框窗口 4. 显示进度对话框 1. 应用样式和主题 改成 android:theme="@android:style/Th ...
- strtol()函数
#include <stdlib.h>#include <stdio.h> int main(){ char a[] = "100"; char b[] = ...
- RequiredFieldValidator的使用
特別說明:1.一個Button要對頁面的多個控件進行驗證,則需要設置button和其它受控控件的ValidationGroup屬性 aspx頁面實例: <tr class="h&quo ...
- python用法笔记(数组(list、touple、dict)、字符串)
1.产生n个全为1的数组a=[1]*n2.字符数字转化int('12')float('12.5')str(123.45)ASCII码转为相应的字符:chr(97)字符转化为相应的ASCII码:ord( ...
- 关于诺顿身份安全2013独立版(Norton Identity Safe)
现在身份安全这货好像从诺顿的套装当中独立出来了,出了中文版.其实诺顿的Web信誉做得还是不错的,当然天朝不要有太大期望.只是公认的做web信誉做得最好的应该就是趋势科技和诺顿,所以诺顿的身份安全也许还 ...
- python windows错误码
在用python删除文件的时候,一直报这个错误,查了 error5的错误是 拒绝访问 在用python删除文件的时候,一直报这个错误,查了 error5的错误是 拒绝访问.那么是删除权限不够?用管理员 ...
- 1001.A+B Format (20)(思路,bug发现及其修改,提交记录)
https://github.com/031502316a/object-oriented/tree/master/1001 ---恢复内容开始--- 1.解题思路 一开始见到题目时,感觉难的就是输出 ...
- [Java线程] Java线程池ExecutorService
示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...
- 将序列设置为字段的默认值 - oracle
1.表结构中log_id 非空.且为主键字段 create table TL_M_QRTZ_LOG( log_id NUMBER(8) not null, job_id NUMBER(8) not n ...
- Routed Events【pluralsight】
Routing Strategies: Direct Bubbling Tunneling WHy use them? Any UIElement can be a listener Common h ...