es6 新增函数及异步函数

Generator

语法

直接执行函数没有效果,必须通过 next() 来移动函数内部的指针,让它指向接下来一个 yield 表达式

有一个前提那就是,yield 必须在 Generator 函数中使用,否则会报错

1
2
3
4
5
6
7
8
9
10
11
function* Great() {
yield 'I';
yield 'am';
yield 'Great';
return 'And you?';
}
let x = Great();
console.log(x.next());
console.log(x.next());
console.log(x.next());
console.log(x.next());
Author: Greatiga
Link: http://example.com/es6/function/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.