//generator是能夠用來控制迭代器的函數,能夠暫停,也能夠任什麼時候候恢復
function * loop () {
for(let i = 0; i < 5; i++) {
yield console.log(i)
}
}
const l = loop()
l.next()
l.next()
function * gen () {
let val
val = (yield [1,2,3]) + 7
console.log(val)
}
const l = gen()
console.log(l.next(10))
l.return(100)
l.next(20)
//捕獲錯誤
function * gen () {
while (true) {
try {
yield 1
}catch (e) {
console.log(e.message)
}
}
}
const g = gen()
console.log(g.next())
console.log(g.next())
console.log(g.next())
g.throw(new Error('ss'))
console.log(g.next())