Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import "babel-polyfill";
- import chai from 'chai'
- chai.should()
- function* justin() {
- /**
- * CALL # 1
- * calls first yield -> runs expression nc(), returns 3.
- * returns back from stack immediately (ie. console.log 1-m never called)
- */
- var m = yield 3;
- console.log('1 - m ', m);
- /**
- * Call #2
- * skips first yield statement
- * calls console.log(1 - m) from above, moves to yield 5 statement
- * returns from stack after calling yield 5
- */
- var m2 = yield 5;
- console.log('2 - m ', m);
- console.log('2 - m2 ', m2);
- /**
- * Call #3
- * skips first yield statement
- * skips second yield statement
- * calls console.log 2-m
- * calls console.log 2-m2
- * And... holy shit it's not 7?? Because the yield has an expression passed into it which returns immediately before {7} is ever executed
- */
- var m3 = yield 7;
- console.log('3 - m ', m);
- console.log('3 - m2 ', m2);
- console.log('3 - m3 ', m3);
- /**
- * Note, m3 console.logs 3-m never called because yield expression 7 is returned on 3rd call
- */
- }
- describe("generator function", function () {
- it("generates thing", function () {
- const gen = justin();
- 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
- 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
- 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
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement