Created
February 4, 2015 06:23
-
-
Save deepak/40137db744bad7aa8f69 to your computer and use it in GitHub Desktop.
next should be called only to advance state. can skip calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
continuation of https://gist.github.com/deepak/8e2f414132bb685e2050