generator 返回一个生成器,生成器里面有next 方法,调用这个方法会返回一个对象 {done:是否迭代完成 value 结果}
function * read() { yield 1; yield 2; yield 3; return 100; } let Iter = read(); console.log(Iter.next()) console.log(Iter.next()) console.log(Iter.next()) console.log(Iter.next())
输出:
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
{ value: 100, done: true }