generator 的使用
第一次调用next 时 传参没有任何意义 打印不出来任何结果
function * read(){ let a = yield 1; console.log(a); let b = yield 2; console.log(b); let c = yield 3; console.log(c); } let it = read('a'); it.next(); // 第一次调用next时 传参没有任何意义
输出:
[Done] exited with code=0 in 0.405 seconds
接下来 调用 next 时 会按照顺序打印出来
function * read(){ let a = yield 1; console.log(a); let b = yield 2; console.log(b); let c = yield 3; console.log(c); } let it = read('a'); it.next(); // 第一次调用next时 传参没有任何意义 it.next('100'); // 这次执行时会打印a的值,a的值是需要调用next方法传递进去的 //打印 a 的值 it.next('200'); it.next('300');
输出:
[Running] node "d:\码云\zhufengjiagoukecheng\Generator\tempCodeRunnerFile.js"
100
200
300