DEV Community

Discussion on: Lazy Recursion Using JavaScript Generators

Collapse
 
gonzalito profile image
Gonzalo Romano

nice!! very nice!!
I would add:
function* take( num, iter ) {
let item = iter.next()
for( let index = 0; index < num && !item.done ; index++) {
yield item.value
item = iter.next()
}
iter.return() // <--
}

so that the generator is resumed after the for finished else it would never finish