Guest User

Untitled

a guest
Nov 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. function *generatorify(dog) {
  2. while (dog.barkCount < 3) {
  3. yield dog.bark()
  4. }
  5. }
  6.  
  7. class chihuahua {
  8. constructor () {
  9. this.barkCount = 0
  10. }
  11. bark () {
  12. this.barkCount++
  13. return 'yap'
  14. }
  15. }
  16.  
  17. const caesar = new chihuahua()
  18. const barkALot = generatorify(caesar)
  19.  
  20. console.log(barkALot.next().value, caesar.barkCount) // 'yap', 1
  21. console.log(barkALot.next().value, caesar.barkCount) // 'yap', 2
  22. console.log(barkALot.next().value, caesar.barkCount) // 'yap', 3
  23. console.log(barkALot.next().done, caesar.barkCount) // true, 3
Add Comment
Please, Sign In to add comment