1️⃣ 基本概念
Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同。
执行 Generator 函数会返回一个遍历器对象,也就是说,Generator 函数除了状态机,还是一个遍历器对象生成函数。返回的遍历器对象,可以依次遍历 Generator 函数内部的每一个状态。
形式上,Generator 函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield表达式,定义不同的内部状态(yield在英语里的意思就是“产出”)。
Generator 函数的调用方法与普通函数一样,也是在函数名后面加上一对圆括号。不同的是,调用 Generator 函数后,该函数并不执行,返回的也不是函数运行结果,而是一个指向内部状态的指针对象
必须调用遍历器对象的next方法,使得指针移向下一个状态。也就是说,每次调用next方法,内部指针就从函数头部或上一次停下来的地方开始执行,直到遇到下一个yield表达式(或return语句)为止。换言之,Generator 函数是分段执行的,yield表达式是暂停执行的标记,而next方法可以恢复执行。
function* generatorFun() {yield 'one';yield 'two';yield 'three';return 'end';}let gf = generatorFun();console.log(gf.next()); // {value: 'one', done: false}console.log(gf.next()); // {value: 'two', done: false}console.log(gf.next()); // {value: 'three', done: false}console.log(gf.next()); // {value: 'end', done: true}console.log(gf.next()); // {value: 'undefined', done: true}
1️⃣ 星号的位置
ES6 没有规定,function关键字与函数名之间的星号,写在哪个位置。这导致下面的写法都能通过。
function * foo(x, y) { ··· }function *foo(x, y) { ··· }function* foo(x, y) { ··· }function*foo(x, y) { ··· }
由于 Generator 函数仍然是普通函数,所以一般的写法是上面的第三种,即星号紧跟在function关键字后面。
1️⃣ yield 表达式
由于 Generator 函数返回的遍历器对象,只有调用 next 方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数。yield表达式就是暂停标志。
遍历器对象的next方法的运行逻辑如下。
1. 遇到 yield 表达式,就暂停执行后面的操作,并将紧跟在 yield 后面的那个表达式的值,作为返回的对象的 value 属性值。2. 下一次调用 next 方法时,再继续往下执行,直到遇到下一个yield表达式。3. 如果没有再遇到新的 yield 表达式,就一直运行到函数结束,直到 return -语句为止,并将return语句后面的表达式的值,作为返回的对象的value属性值。4. 如果该函数没有return语句,则返回的对象的value属性值为undefined。
需要注意的是,yield表达式后面的表达式,只有当调用 next 方法、内部指针指向该语句时才会执行,因此等于为 JavaScript 提供了手动的“惰性求值”(Lazy Evaluation)的语法功能。
function* generatorFun() {console.log(1);yield 'one';console.log(2);yield 'two';console.log(3);yield 'three';console.log(4);return 'end';}let gf = generatorFun();// 1console.log(gf.next()); // {value: 'one', done: false}// 2console.log(gf.next()); // {value: 'two', done: false}// 3console.log(gf.next()); // {value: 'three', done: false}// 4console.log(gf.next()); // {value: 'end', done: true}console.log(gf.next()); // {value: 'undefined', done: true}
let gf = generatorFun();gf.next();function* generatorFun() {setTimeout(() => {console.log('one');gf.next(); // one 执行有结果后调用 next 执行 two}, 1000);yield 'one';setTimeout(() => {console.log('two');gf.next(); // two 执行有结果后调用 next 执行 three}, 2000);yield 'twosetTimeout(() => {console.log('three');gf.next(); // three 执行有结果后调用 next 执行最后的逻辑}, 3000);yield 'three';setTimeout(() => {console.log('执行最后');}, 3000);return 'end';}
1️⃣ next 方法的参数
yield 表达式本身没有返回值,或者说总是返回 undefined。next 方法可以带一个参数,该参数就会被当作上一个 yield 表达式的返回值。
function* dataConsumer() {console.log('Started');console.log(`1. ${yield}`);console.log(`2. ${yield}`);return 'result';}let genObj = dataConsumer();genObj.next();// StartedgenObj.next('a');// 1. agenObj.next('b');// 2. b
1️⃣ for…of 循环
for…of 循环可以自动遍历 Generator 函数运行时生成的 Iterator 对象,且此时不再需要调用 next 方法。
function* foo() {yield 1;yield 2;yield 3;yield 4;yield 5;return 6;}for (let v of foo()) {console.log(v);}// 1 2 3 4 5
上面代码使用 for…of 循环,依次显示 5 个 yield 表达式的值。这里需要注意,一旦 next方法的返回对象的 done 属性为 true,for…of 循环就会中止,且不包含该返回对象,所以上面代码的 return 语句返回的 6,不包括在 for…of 循环之中。
除了 for…of 循环以外,扩展运算符(…)、解构赋值和 Array.from 方法内部调用的,都是遍历器接口。这意味着,它们都可以将 Generator 函数返回的 Iterator 对象,作为参数。
