ES6里的生成器Generators
生成器的定义:
使用关键字function* 来定义一个生成器
生成器内部使用yield关键字来做输入输出的操作
假如yield后面有return的操作,那么return后面的yield都会无效
yield 的操作是先输出,[然后再读一个内容,假如你有输入的话]
yield 是双向的不阻断的,return 是单向只读的,return 完后立即终止
例子:
function* communication(){
var name = yield 'what is your name ?';
var age = yield 'what is your age ?';
return `$ {name}'s age is ${age}`;
var weight = yield 'what is your weight?'
}
var talk = communication();
talk.next();
talk.next('john');
talk.next('18');
talk.next();