1️⃣ 基本概念

Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同。
执行 Generator 函数会返回一个遍历器对象,也就是说,Generator 函数除了状态机,还是一个遍历器对象生成函数。返回的遍历器对象,可以依次遍历 Generator 函数内部的每一个状态。
形式上,Generator 函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield表达式,定义不同的内部状态(yield在英语里的意思就是“产出”)。
Generator 函数的调用方法与普通函数一样,也是在函数名后面加上一对圆括号。不同的是,调用 Generator 函数后,该函数并不执行,返回的也不是函数运行结果,而是一个指向内部状态的指针对象
必须调用遍历器对象的next方法,使得指针移向下一个状态。也就是说,每次调用next方法,内部指针就从函数头部或上一次停下来的地方开始执行,直到遇到下一个yield表达式(或return语句)为止。换言之,Generator 函数是分段执行的,yield表达式是暂停执行的标记,而next方法可以恢复执行。

  1. function* generatorFun() {
  2. yield 'one';
  3. yield 'two';
  4. yield 'three';
  5. return 'end';
  6. }
  7. let gf = generatorFun();
  8. console.log(gf.next()); // {value: 'one', done: false}
  9. console.log(gf.next()); // {value: 'two', done: false}
  10. console.log(gf.next()); // {value: 'three', done: false}
  11. console.log(gf.next()); // {value: 'end', done: true}
  12. console.log(gf.next()); // {value: 'undefined', done: true}

1️⃣ 星号的位置

ES6 没有规定,function关键字与函数名之间的星号,写在哪个位置。这导致下面的写法都能通过。

  1. function * foo(x, y) { ··· }
  2. function *foo(x, y) { ··· }
  3. function* foo(x, y) { ··· }
  4. function*foo(x, y) { ··· }

由于 Generator 函数仍然是普通函数,所以一般的写法是上面的第三种,即星号紧跟在function关键字后面。

1️⃣ yield 表达式

由于 Generator 函数返回的遍历器对象,只有调用 next 方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数。yield表达式就是暂停标志。
遍历器对象的next方法的运行逻辑如下。

  1. 1. 遇到 yield 表达式,就暂停执行后面的操作,并将紧跟在 yield 后面的那个表达式的值,作为返回的对象的 value 属性值。
  2. 2. 下一次调用 next 方法时,再继续往下执行,直到遇到下一个yield表达式。
  3. 3. 如果没有再遇到新的 yield 表达式,就一直运行到函数结束,直到 return -语句为止,并将return语句后面的表达式的值,作为返回的对象的value属性值。
  4. 4. 如果该函数没有return语句,则返回的对象的value属性值为undefined。

需要注意的是,yield表达式后面的表达式,只有当调用 next 方法、内部指针指向该语句时才会执行,因此等于为 JavaScript 提供了手动的“惰性求值”(Lazy Evaluation)的语法功能。

  1. function* generatorFun() {
  2. console.log(1);
  3. yield 'one';
  4. console.log(2);
  5. yield 'two';
  6. console.log(3);
  7. yield 'three';
  8. console.log(4);
  9. return 'end';
  10. }
  11. let gf = generatorFun();
  12. // 1
  13. console.log(gf.next()); // {value: 'one', done: false}
  14. // 2
  15. console.log(gf.next()); // {value: 'two', done: false}
  16. // 3
  17. console.log(gf.next()); // {value: 'three', done: false}
  18. // 4
  19. console.log(gf.next()); // {value: 'end', done: true}
  20. console.log(gf.next()); // {value: 'undefined', done: true}
  1. let gf = generatorFun();
  2. gf.next();
  3. function* generatorFun() {
  4. setTimeout(() => {
  5. console.log('one');
  6. gf.next(); // one 执行有结果后调用 next 执行 two
  7. }, 1000);
  8. yield 'one';
  9. setTimeout(() => {
  10. console.log('two');
  11. gf.next(); // two 执行有结果后调用 next 执行 three
  12. }, 2000);
  13. yield 'two
  14. setTimeout(() => {
  15. console.log('three');
  16. gf.next(); // three 执行有结果后调用 next 执行最后的逻辑
  17. }, 3000);
  18. yield 'three';
  19. setTimeout(() => {
  20. console.log('执行最后');
  21. }, 3000);
  22. return 'end';
  23. }

1️⃣ next 方法的参数

yield 表达式本身没有返回值,或者说总是返回 undefined。next 方法可以带一个参数,该参数就会被当作上一个 yield 表达式的返回值。

  1. function* dataConsumer() {
  2. console.log('Started');
  3. console.log(`1. ${yield}`);
  4. console.log(`2. ${yield}`);
  5. return 'result';
  6. }
  7. let genObj = dataConsumer();
  8. genObj.next();
  9. // Started
  10. genObj.next('a');
  11. // 1. a
  12. genObj.next('b');
  13. // 2. b

1️⃣ for…of 循环

for…of 循环可以自动遍历 Generator 函数运行时生成的 Iterator 对象,且此时不再需要调用 next 方法。

  1. function* foo() {
  2. yield 1;
  3. yield 2;
  4. yield 3;
  5. yield 4;
  6. yield 5;
  7. return 6;
  8. }
  9. for (let v of foo()) {
  10. console.log(v);
  11. }
  12. // 1 2 3 4 5

上面代码使用 for…of 循环,依次显示 5 个 yield 表达式的值。这里需要注意,一旦 next方法的返回对象的 done 属性为 true,for…of 循环就会中止,且不包含该返回对象,所以上面代码的 return 语句返回的 6,不包括在 for…of 循环之中。
除了 for…of 循环以外,扩展运算符(…)、解构赋值和 Array.from 方法内部调用的,都是遍历器接口。这意味着,它们都可以将 Generator 函数返回的 Iterator 对象,作为参数。