Last active
August 29, 2015 14:14
-
-
Save deepak/8e2f414132bb685e2050 to your computer and use it in GitHub Desktop.
simple generator
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 () { | |
// iterator.next(price++); // throws a "Generator is already running" error | |
return price++ | |
} | |
let oops = function () { | |
throw new Error("oops!"); | |
} | |
let stockTicker = function*() { | |
yield stockPrice(); | |
yield stockPrice(); | |
// throw new Error("oops!"); // same as calling "oops" as below | |
oops(); | |
}; | |
var iterator = stockTicker(); | |
console.log(iterator.next()); //{"value":10,"done":false} | |
console.log(iterator.next()); // {"value":11,"done":false} | |
console.log(iterator.next()); // oops! |
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/f41c622efc574ebab7c1