Created
February 4, 2015 07:02
-
-
Save deepak/ffe01c81900d12f95658 to your computer and use it in GitHub Desktop.
error handling in ES6 generators
This file contains 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++); | |
}, 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} |
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/40137db744bad7aa8f69