Skip to content

Instantly share code, notes, and snippets.

@deepak
Created February 4, 2015 07:02
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/ffe01c81900d12f95658 to your computer and use it in GitHub Desktop.
Save deepak/ffe01c81900d12f95658 to your computer and use it in GitHub Desktop.
error handling in ES6 generators
let price = 10;
let stockPrice = function () {
setTimeout(function() {
iterator.next(price++);
}, 300);
}
let errorInStockPrice = function() {
setTimeout(function() {
iterator.throw(Error("oops!"));
}, 300);
}
let stockTicker = function*() {
try {
price = yield stockPrice();
console.log("price is " + price); //=> price is 10
price = yield stockPrice();
console.log("price is " + price); //=> price is 10
price = yield errorInStockPrice();
console.log("price is " + price); //=> price is 10
} catch (ex) {
console.log("error in stock-price: " + ex); //=> error in stock-price: Error: oops!
}
};
var iterator = stockTicker();
console.log(iterator.next()); //=> {"done":false}
@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