Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function *generatorify(dog) {
- while (dog.barkCount < 3) {
- yield dog.bark()
- }
- }
- class chihuahua {
- constructor () {
- this.barkCount = 0
- }
- bark () {
- this.barkCount++
- return 'yap'
- }
- }
- const caesar = new chihuahua()
- const barkALot = generatorify(caesar)
- console.log(barkALot.next().value, caesar.barkCount) // 'yap', 1
- console.log(barkALot.next().value, caesar.barkCount) // 'yap', 2
- console.log(barkALot.next().value, caesar.barkCount) // 'yap', 3
- console.log(barkALot.next().done, caesar.barkCount) // true, 3
Add Comment
Please, Sign In to add comment