Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import "babel-polyfill";
  2.  
  3. import chai from 'chai'
  4.  
  5. chai.should()
  6.  
  7.  
  8. function* justin() {
  9. /**
  10. * CALL # 1
  11. * calls first yield -> runs expression nc(), returns 3.
  12. * returns back from stack immediately (ie. console.log 1-m never called)
  13. */
  14. var m = yield 3;
  15. console.log('1 - m ', m);
  16.  
  17.  
  18. /**
  19. * Call #2
  20. * skips first yield statement
  21. * calls console.log(1 - m) from above, moves to yield 5 statement
  22. * returns from stack after calling yield 5
  23. */
  24. var m2 = yield 5;
  25. console.log('2 - m ', m);
  26. console.log('2 - m2 ', m2);
  27.  
  28. /**
  29. * Call #3
  30. * skips first yield statement
  31. * skips second yield statement
  32. * calls console.log 2-m
  33. * calls console.log 2-m2
  34. * And... holy shit it's not 7?? Because the yield has an expression passed into it which returns immediately before {7} is ever executed
  35. */
  36. var m3 = yield 7;
  37. console.log('3 - m ', m);
  38. console.log('3 - m2 ', m2);
  39. console.log('3 - m3 ', m3);
  40.  
  41. /**
  42. * Note, m3 console.logs 3-m never called because yield expression 7 is returned on 3rd call
  43. */
  44. }
  45.  
  46. describe("generator function", function () {
  47. it("generates thing", function () {
  48.  
  49. const gen = justin();
  50.  
  51. console.log('value ', gen.next().value); // call #1 prints 1 (all call's below the first yield are not executed bc the stack has already emptied
  52. console.log('value ', gen.next().value); // call #2 prints 2 (all call's below the first yield are not executed bc the stack has already emptied
  53. console.log('value ', gen.next("holy shit, some other expression!").value); // call #1 prints 1 (all call's below the first yield are not executed bc the stack has already emptied
  54.  
  55. });
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement