Skip to content

Instantly share code, notes, and snippets.

@deepak
Created February 4, 2015 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak/40137db744bad7aa8f69 to your computer and use it in GitHub Desktop.
Save deepak/40137db744bad7aa8f69 to your computer and use it in GitHub Desktop.
next should be called only to advance state. can skip calls
let price = 10;
let stockPrice = function () {
setTimeout(function() {
iterator.next(price++);
console.log(price); //=> 10
// why is this logged ? next does not behave like return ?
// and it is always 10. "price++" did not increment value of price
}, 300);
}
let oops = function () {
throw new Error("oops!");
}
let stockTicker = function*() {
price = yield stockPrice();
console.log("price is " + price); //=> price is 10
price = yield stockPrice();
console.log("price is " + price); //=> price is 10
// throw new Error("oops!");
oops();
};
var iterator = stockTicker();
console.log(iterator.next()); //=> {"done":false}
// calling next here is bad. because next is called from the `stockPrice` function
// calling next here would give the next value, even though stockPrice has a setTimeout callback running
//console.log(iterator.next());
//console.log(iterator.next());
@deepak
Copy link
Author

deepak commented Feb 4, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment